Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mdmintz opened this issue Jun 27, 2024 · 7 comments · Fixed by #2882
Closed

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

mdmintz opened this issue Jun 27, 2024 · 7 comments · Fixed by #2882
Assignees
Labels
enhancement Making things better UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode

Comments

@mdmintz
Copy link
Member

mdmintz commented Jun 27, 2024

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.)

@mdmintz mdmintz added enhancement Making things better UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode labels Jun 27, 2024
@mdmintz mdmintz self-assigned this Jun 27, 2024
@goldananas
Copy link

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
Copy link
Member Author

mdmintz commented Jun 28, 2024

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

@PankajSavaliya
Copy link

PankajSavaliya commented Jul 2, 2024

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
Copy link
Member Author

mdmintz commented Jul 2, 2024

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
Copy link

@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
Copy link
Member Author

mdmintz commented Jul 2, 2024

For uc_gui_handle_cf() to work, pyautogui needs permission to click things on your desktop. I saw this related item: asweigart/pyautogui#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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Making things better UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants