SeleniumBase icon indicating copy to clipboard operation
SeleniumBase copied to clipboard

There's a new UC Mode video showing how it works

Open mdmintz opened this issue 1 year ago • 28 comments

There's a new UC Mode video showing how it works, and how it fixes bugs with undetected-chromedriver: https://www.youtube.com/watch?v=5dMFI3e85ig

Note the improvements made:

  • Driver version-detection & management.
  • Allowing mismatched browser/driver versions.
  • Automatically changing the user agent to prevent detection. (HeadlessChrome to Chrome)
  • Automatically disconnecting chromedriver from Chrome as needed. (And reconnecting)
  • Multithreading tests in parallel via pytest-xdist.
  • Adjusting configuration based on the environment. (Linux/Ubuntu vs Windows vs macOS)
  • Explaining how to set a custom user-data-dir correctly.
  • Has options for setting proxy and proxy-with-auth.

Many of these fixes can be back-ported easily to undetected-chromedriver. (Explanation in the video of how to do that, such as using requests.get(url) before navigating to a URL to know if you need to disconnect/reconnect the driver from Chrome.)

Here's a script with retries and a captcha-click failsafe for bypassing detection:

from seleniumbase import SB

with SB(uc=True) as sb:
    sb.driver.get("https://nowsecure.nl/#relax")
    sb.sleep(1)
    if not sb.is_text_visible("OH YEAH, you passed!", "h1"):
        sb.get_new_driver(undetectable=True)
        sb.driver.get("https://nowsecure.nl/#relax")
        sb.sleep(1)
    if not sb.is_text_visible("OH YEAH, you passed!", "h1"):
        if sb.is_element_visible('iframe[src*="challenge"]'):
            with sb.frame_switch('iframe[src*="challenge"]'):
                sb.click("span.mark")
                sb.sleep(2)
    sb.activate_demo_mode()
    sb.assert_text("OH YEAH, you passed!", "h1", timeout=3)

The presentation slides were autogenerated by running: https://github.com/seleniumbase/SeleniumBase/blob/master/examples/presenter/uc_presentation.py

Here are some existing examples that use --uc mode:

  • https://github.com/seleniumbase/SeleniumBase/blob/master/examples/verify_undetected.py
  • https://github.com/seleniumbase/SeleniumBase/blob/master/examples/uc_cdp_events.py
  • https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_uc_mode.py

mdmintz avatar Oct 27 '23 17:10 mdmintz

hi @mdmintz . I tried this code but on windows 11 using your latest version, doesnt seem to work. I never pass the check! What i'm missing? i also tried to add the headless=True flag but the browser is still opening. tried to uninstall my previous selenium version also didnt fix the problem

idan1109 avatar Oct 30 '23 12:10 idan1109

@idan1109 Show me the code you used. And the output of pip list. selenium is part of seleniumbase dependencies, so if you installed it correctly, it would automatically update the selenium version to the required one during install.

mdmintz avatar Oct 30 '23 13:10 mdmintz

Hi @mdmintz , I executed the code you presented above and did not pass the check. It stops after clicking the 'verify' checkbox. The version is selenium==4.14.0 seleniumbase==4.20.8. I used Chrome 119.0.6045.105 in Docker with xvfb-run.

wuvei avatar Nov 01 '23 03:11 wuvei

@yczheng0 Thanks for catching that and providing details to reproduce the issue! It was a regression in version 4.20.8. Upgrade to 4.20.9 - https://github.com/seleniumbase/SeleniumBase/releases/tag/v4.20.9 for the fixed version!

mdmintz avatar Nov 01 '23 06:11 mdmintz

Thanks @mdmintz for your great work! It works.

wuvei avatar Nov 01 '23 07:11 wuvei

Hi @mdmintz , sorry to bother again. When the code executes to click the verify checkbox, the page will be kept in a captcha loop, i.e. always requiring clicking the checkbox. Same problem mentioned in undetected-chromedriver/#1543.

wuvei avatar Nov 06 '23 06:11 wuvei

Hi @yczheng0, Assuming everything is installed correctly and you have UC Mode activated, it shouldn't be forcing you to click the checkbox at all. There are bonus methods that you can use to help:

driver.uc_open(url)
driver.uc_open_with_tab(url)
driver.uc_open_with_reconnect(url, reconnect_time=None)
driver.uc_click(selector)

(As talked about in https://www.youtube.com/watch?v=5dMFI3e85ig)

mdmintz avatar Nov 06 '23 13:11 mdmintz

@yczheng0 is correct. I also did try with all the four methods mentioned by you above. Also both the methods in youtube video including using sb context. But i think it still does get detected at "https://nowsecure.nl/" by cloudfare. It won't go past clicking on the checkbox.

sujaykhandekar avatar Nov 07 '23 00:11 sujaykhandekar

@yczheng0 Which example are you running? SeleniumBase/examples/verify_undetected.py is working for me.

mdmintz avatar Nov 07 '23 05:11 mdmintz

@mdmintz I ran SeleniumBase/examples/raw_uc_mode.py, which resulted in infinite loop. SeleniumBase/examples/verify_undetected.py is working without clicking.

wuvei avatar Nov 07 '23 06:11 wuvei

@yczheng0

I ran SeleniumBase/examples/raw_uc_mode.py, which resulted in infinite loop.

That shouldn't happen if you just run python raw_uc_mode.py.

SeleniumBase/examples/verify_undetected.py is working without clicking.

Great!

mdmintz avatar Nov 07 '23 06:11 mdmintz

Hi @mdmintz i am able to get detected on a website which is cloudflare turnstile enabled Screenshot 2023-12-18 203739 i can share the link of the webpage

diptilenka avatar Dec 18 '23 15:12 diptilenka

@diptilenka Show me the code you used. It sounds like you modified the script from examples/verify_undetected.py with a different URL, but you never modified the assertion statement to look for something else. The OH YEAH, you passed! message is unique to https://nowsecure.nl/#relax

mdmintz avatar Dec 18 '23 15:12 mdmintz

@mdmintz Yes i changed the URL , but the cloud flare shown like this , after clicking its saying failed

Screenshot 2023-12-18 210632

diptilenka avatar Dec 18 '23 15:12 diptilenka

@diptilenka That's a form turnstile. (I can see the password field above it.) Those work differently from turnstiles that aren't meant to be clicked at all if the system thinks you're human. The form turnstile must be clicked by both humans and bots, so it works a bit differently. See the examples for clicking form turnstiles:

  • https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_turnstile.py
  • https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_form_turnstile.py

mdmintz avatar Dec 18 '23 15:12 mdmintz

@mdmintz I will look into those examples, but should i fill the password and username before clicking "Verify you are human" checkbox for showing success?

If we open that website in normal human way after 12 seconds it automatically shows success, and no click on that check box shown

diptilenka avatar Dec 18 '23 15:12 diptilenka

@diptilenka

If we open that website in normal human way after 12 seconds it automatically shows success

If that's actually the case, then use sb.driver.uc_open_with_reconnect(URL, 15) so that it automatically checks the box for you. (It adds a little extra time to account for the new tab and page load.)

If that's not the case, do the turnstile click first before filling out the form. (See the examples.)

mdmintz avatar Dec 18 '23 16:12 mdmintz

@mdmintz Thanks It worked and moving to login and other parts. will ping you once i stuck in somewhere. :)

But at first its open to verify as human and after some seconds it opens another windows and shown success.

diptilenka avatar Dec 18 '23 16:12 diptilenka

Hey, i want to tell you thanks, you're the best, did the great work. A couple of days ago i interfered with some problem, i can’t log in twitch with seleniumbase, it detects me everytime i try, whatever i did, can you give me a clue, is it possible? 😅 I use python 3.11 and macOS m series

kaster3 avatar Jan 29 '24 00:01 kaster3

The two main causes of getting detected in UC Mode (which are both easily handled) are:

  • Timing (The UC Mode methods let you customize that when the default values aren't good enough for your env.)
  • Not using the driver.uc_click(selector) method when you need to remain undetected while clicking something.

To see if UC Mode can work on your site (before adjusting for timing & special methods), load your site with the following script, which drops in a Python breakpoint() while chromedriver is disconnected from Chrome.

from seleniumbase import SB

with SB(uc=True) as sb:
    sb.driver.uc_open_with_reconnect(URL, reconnect_time="breakpoint")

From the breakpoint, manually perform that actions that you want. If you don't get detected, then good news: Once you adjust the timing and use sb.driver.uc_click(selector) when you need to click, then you can make your script work. Also note that there are other methods that you may need to use, such as sb.driver.uc_switch_to_frame("iframe") when you need to safely enter an iframe without getting detected, etc.

After you've performed your manual actions in the web browser, type c in the console and press ENTER to leave the breakpoint. (While you're in that special disconnected breakpoint, you won't be able to send webdriver commands to Chrome, but Chrome can't see webdriver either, which is how you evade detection.)

See https://github.com/seleniumbase/SeleniumBase/issues/2384#issue-2054672141 for more info about timing customizations and the special breakpoint in UC Mode.

Also see SeleniumBase/examples/raw_form_turnstile.py for an example of special clicks in UC Mode.

mdmintz avatar Jan 29 '24 01:01 mdmintz

Thanks a lot for the detailed response, but, unfortunately, it seems doesn't work even with this script and manual actions:

from seleniumbase import SB

with SB(uc=True) as sb: sb.driver.uc_open_with_reconnect(URL, reconnect_time="breakpoint")

The twitch site detected me every time and notifies me, when I click log in button or register button, after filled the forms with message: "you have a old browser version"

kaster3 avatar Jan 29 '24 10:01 kaster3

The UC Mode video tutorial now has over 10k views on YouTube:


Screenshot 2024-02-23 at 8 15 10 AM

mdmintz avatar Feb 23 '24 13:02 mdmintz

Thanks for the video instruction @mdmintz ! In the video you briefly mentioned we need "reconnect()" when evading CF because it has something to do with the session storage in chrome.

Could you elaborate a little more on what that means? It would be even better if there's some example for illustration purposes.

Also do you know if this is the same reason we need different user profiles when "UC" vs. "Non UC"?

Tongcheng avatar Mar 13 '24 01:03 Tongcheng

@Tongcheng There's no mention of "session storage" anywhere in the video. I think you need to rewatch it.

The reconnect() is for hiding Selenium from Chrome to prevent anti-bot services from detecting Selenium.

The user_data_dir of regular Chrome vs UC Chrome are different. That's why you can't "cross the streams".

mdmintz avatar Mar 13 '24 01:03 mdmintz

UC Mode Video 2 is ready!

https://www.youtube.com/watch?v=2pTpBtaE7SQ

mdmintz avatar Apr 25 '24 03:04 mdmintz

some website detect cdc_ variable, and every-time selenium connect to chrome drive, it add the variable back, sometimes remove_cdc_props_as_needed() is not even working

guocity avatar May 18 '24 16:05 guocity