seleniumbase / SeleniumBase

📊 Python's all-in-one framework for web crawling, scraping, testing, and reporting. Supports pytest. UC Mode provides stealth. Includes many tools.
https://seleniumbase.io
MIT License
4.45k stars 908 forks source link

Add support for `uc_gui_handle_cf()` with `Driver()` and `DriverContext()` formats #2879

Closed mdmintz closed 4 days ago

mdmintz commented 5 days ago

Add support for uc_gui_handle_cf() with Driver() and DriverContext() formats

Currently, if running this code:

from seleniumbase import DriverContext

with DriverContext(uc=True) as driver:
    url = "https://www.virtualmanager.com/en/login"
    driver.uc_open_with_reconnect(url, 4)
    driver.uc_gui_handle_cf()  # Ready if needed!
    driver.assert_element('input[name*="email"]')
    driver.assert_element('input[name*="login"]')

That leads to this stack trace:

  File "/Users/michael/github/SeleniumBase/seleniumbase/core/browser_launcher.py", line 4025, in <lambda>
    lambda *args, **kwargs: uc_gui_handle_cf(
                            ^^^^^^^^^^^^^^^^^
  File "/Users/michael/github/SeleniumBase/seleniumbase/core/browser_launcher.py", line 651, in uc_gui_handle_cf
    install_pyautogui_if_missing()
  File "/Users/michael/github/SeleniumBase/seleniumbase/core/browser_launcher.py", line 559, in install_pyautogui_if_missing
    verify_pyautogui_has_a_headed_browser()
  File "/Users/michael/github/SeleniumBase/seleniumbase/core/browser_launcher.py", line 552, in verify_pyautogui_has_a_headed_browser
    if sb_config.headless or sb_config.headless2:
       ^^^^^^^^^^^^^^^^^^
AttributeError: module 'seleniumbase.config' has no attribute 'headless'

Here's the workaround for now using SB(): (Which includes the virtual display needed on Linux)

from seleniumbase import SB

with SB(uc=True) as sb:
    url = "https://www.virtualmanager.com/en/login"
    sb.uc_open_with_reconnect(url, 4)
    sb.uc_gui_handle_cf()  # Ready if needed!
    sb.assert_element('input[name*="email"]')
    sb.assert_element('input[name*="login"]')

Once this ticket is resolved, Linux users who use Driver() or DriverContext formats in UC Mode will still need to set pyautogui._pyautogui_x11._display to Xlib.display.Display(os.environ['DISPLAY']) on Linux in order to sync up pyautogui with the X11 virtual display after calling sbvirtualdisplay.Display(visible=True, size=(1366, 768), backend="xvfb", use_xauth=True).start(). (For Xlib, use import Xlib.display after pip install python-xlib.)

goldananas commented 5 days ago

Hey, so that's the error I was facing the other day in my last comment . I could have been more meaningful. Anyway thanks for fixing it.

mdmintz commented 4 days ago

This was resolved in 4.28.1 - https://github.com/seleniumbase/SeleniumBase/releases/tag/v4.28.1

PankajSavaliya commented 11 hours ago

driver.uc_open(url) driver.uc_gui_handle_cf()

In Mac uc_gui_handle_cf not working, i'm getting a captcha for GitLab login even nothing has changed. seleniumbase==4.28.3

driver = Driver(uc=True,)
    try:

        driver.uc_open("https://gitlab.com/users/sign_in")
        driver.uc_gui_handle_cf()

        driver.sleep(50)
        print("done")

    except Exception as e:
        print(e)
    finally:
        driver.quit()
mdmintz commented 9 hours ago

Working for me:

from seleniumbase import Driver

with Driver(uc=True) as driver:
    url = "https://gitlab.com/users/sign_in"
    driver.uc_open_with_reconnect(url, 4)
    driver.uc_gui_handle_cf()  # Not always needed
    driver.assert_text("Username", '[for="user_login"]', timeout=3)
    driver.assert_element('label[for="user_login"]')
    driver.highlight('button:contains("Sign in")')
    driver.highlight('h1:contains("GitLab.com")')
PankajSavaliya commented 7 hours ago

@mdmintz I have used the same code on my Mac, but it still does not resolve the captcha in GitLab.

As per screenshots The Python app always jumps but does not start. I don't know why I need to give permission for the external library that is already installed while it was added uc_gui_handle_cf into the code?

I have not changed anything, including the fingerprint, VPN, or proxy, yet I still get this captcha. It should work as expected.

Screenshot 2024-07-02 at 6 53 29 PM Screenshot 2024-07-02 at 6 53 40 PM
mdmintz commented 7 hours ago

For uc_gui_handle_cf() to work, pyautogui needs permission to click things on your desktop. I saw this related item: https://github.com/asweigart/pyautogui/issues/834 (Maybe more in https://github.com/asweigart/pyautogui). If pyautogui is asking for permission, then it probably doesn't have it yet, and it needs it.

On macOS / Windows, the CAPTCHA should be bypassed automatically without needing to click it (unless they already flagged your IP Address, in which case nothing can be done). For slow internet connections, you may need to increase the number in uc_open_with_reconnect(url, 4).

PankajSavaliya commented 6 hours ago

I have tried with proxy and VPN also even though it gives me a captcha as per the screenshot. let me resolve the permission things first, if you have any solution even getting this error please let me know what should I check.