Merge pull request #2987 from seleniumbase/update-mobile-mode-and-uc-mode

Update Mobile Mode and UC Mode
This commit is contained in:
Michael Mintz 2024-08-02 22:43:45 -04:00 committed by GitHub
commit e8c2147036
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 160 additions and 102 deletions

View File

@ -5,7 +5,7 @@ driver = Driver(uc=True, log_cdp=True)
try:
url = "seleniumbase.io/apps/turnstile"
driver.uc_open_with_reconnect(url, 2)
driver.uc_gui_handle_cf()
driver.uc_gui_handle_captcha()
driver.sleep(3)
pprint(driver.get_log("performance"))
finally:

View File

@ -13,7 +13,7 @@ with SB(uc=True, test=True) as sb:
sb.highlight_click('input[value="AR"] + span')
sb.click('input[value="cc"] + span')
sb.scroll_to('div[class*="cf-turnstile"]')
sb.uc_gui_handle_cf()
sb.uc_gui_handle_captcha()
sb.highlight("img#captcha-success", timeout=3)
sb.highlight_click('button:contains("Request & Pay")')
sb.highlight("img#submit-success")

View File

@ -1,10 +1,7 @@
"""
UC Mode now has uc_gui_click_cf(), which uses PyAutoGUI.
An incomplete UserAgent forces CAPTCHA-solving on macOS.
"""
import sys
from seleniumbase import SB
# An incomplete UserAgent forces CAPTCHA-solving on macOS
agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/126.0.0.0"
if "linux" in sys.platform or "win32" in sys.platform:
agent = None # Use the default UserAgent
@ -12,7 +9,7 @@ if "linux" in sys.platform or "win32" in sys.platform:
with SB(uc=True, test=True, rtf=True, agent=agent) as sb:
url = "https://gitlab.com/users/sign_in"
sb.uc_open_with_reconnect(url, 4)
sb.uc_gui_click_cf() # Ready if needed!
sb.uc_gui_click_captcha() # Only if needed
sb.assert_element('label[for="user_login"]')
sb.assert_element('input[data-testid*="username"]')
sb.assert_element('input[data-testid*="password"]')

View File

@ -1,10 +1,7 @@
"""
UC Mode now has uc_gui_handle_cf(), which uses PyAutoGUI.
An incomplete User-Agent forces CAPTCHA-solving on macOS.
"""
import sys
from seleniumbase import SB
# An incomplete UserAgent forces CAPTCHA-solving on macOS
agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/126.0.0.0"
if "linux" in sys.platform or "win32" in sys.platform:
agent = None # Use the default UserAgent
@ -12,7 +9,7 @@ if "linux" in sys.platform or "win32" in sys.platform:
with SB(uc=True, test=True, rtf=True, agent=agent) as sb:
url = "https://gitlab.com/users/sign_in"
sb.uc_open_with_reconnect(url, 4)
sb.uc_gui_handle_cf() # Ready if needed!
sb.uc_gui_handle_captcha() # Only if needed
sb.assert_element('label[for="user_login"]')
sb.assert_element('input[data-testid*="username"]')
sb.assert_element('input[data-testid*="password"]')

View File

@ -3,7 +3,15 @@ from seleniumbase import SB
with SB(uc=True, test=True) as sb:
url = "https://seleniumbase.io/apps/recaptcha"
sb.uc_open_with_reconnect(url)
sb.uc_gui_click_captcha()
sb.uc_gui_handle_captcha() # Try with TAB + SPACEBAR
sb.assert_element("img#captcha-success", timeout=3)
sb.set_messenger_theme(location="top_left")
sb.post_message("SeleniumBase wasn't detected", duration=3)
with SB(uc=True, test=True) as sb:
url = "https://seleniumbase.io/apps/recaptcha"
sb.uc_open_with_reconnect(url)
sb.uc_gui_click_captcha() # Try with PyAutoGUI Click
sb.assert_element("img#captcha-success", timeout=3)
sb.set_messenger_theme(location="top_left")
sb.post_message("SeleniumBase wasn't detected", duration=3)

View File

@ -13,7 +13,7 @@ class CDPTests(BaseCase):
)
def click_turnstile_and_verify(sb):
sb.uc_gui_handle_cf()
sb.uc_gui_handle_captcha()
sb.assert_element("img#captcha-success", timeout=3)
sb.highlight("img#captcha-success", loops=8)

View File

@ -1077,12 +1077,12 @@ driver.uc_gui_write(text) # Similar to uc_gui_press_keys(), but faster
driver.uc_gui_click_x_y(x, y, timeframe=0.25) # PyAutoGUI click screen
driver.uc_gui_click_captcha(frame="iframe", retry=False, blind=False)
# driver.uc_gui_click_cf(frame="iframe", retry=False, blind=False)
# driver.uc_gui_click_rc(frame="iframe", retry=False, blind=False)
driver.uc_gui_click_rc(frame="iframe", retry=False, blind=False) # reC
driver.uc_gui_click_cf(frame="iframe", retry=False, blind=False) # CFT
driver.uc_gui_handle_cf(frame="iframe") # PyAutoGUI click CF Turnstile
driver.uc_gui_handle_captcha(frame="iframe") # (Auto-detects the CAPTCHA)
# driver.uc_gui_handle_cf(frame="iframe") # PyAutoGUI click CF Turnstile
# driver.uc_gui_handle_rc(frame="iframe") # PyAutoGUI click G. reCAPTCHA
driver.uc_switch_to_frame(frame="iframe") # Stealthy switch_to_frame()
```

View File

@ -45,7 +45,9 @@ with SB(uc=True) as sb:
sb.uc_open_with_reconnect(url, 4)
```
👤 Here's a longer example, which includes a special click if the CAPTCHA isn't bypassed on the initial page load:
(Note: If running UC Mode scripts on headless Linux machines, then you'll need to use the <b><code translate="no">SB</code></b> manager instead of the <b><code translate="no">Driver</code></b> manager because the <b><code translate="no">SB</code></b> manager includes a special virtual display that allows for <b><code translate="no">PyAutoGUI</code></b> actions.)
👤 Here's a longer example, which includes a special <b><code translate="no">PyAutoGUI</code></b> click if the CAPTCHA isn't bypassed on the initial page load:
```python
from seleniumbase import SB
@ -71,7 +73,7 @@ from seleniumbase import SB
with SB(uc=True, test=True) as sb:
url = "https://seleniumbase.io/apps/turnstile"
sb.uc_open_with_reconnect(url, reconnect_time=2)
sb.uc_gui_handle_cf()
sb.uc_gui_handle_captcha()
sb.assert_element("img#captcha-success", timeout=3)
sb.set_messenger_theme(location="top_left")
sb.post_message("SeleniumBase wasn't detected", duration=3)
@ -79,7 +81,7 @@ with SB(uc=True, test=True) as sb:
<img src="https://seleniumbase.github.io/other/turnstile_click.jpg" title="SeleniumBase" width="440">
If running on a Linux server, `uc_gui_handle_cf()` might not be good enough. Switch to `uc_gui_click_cf()` to be more stealthy. You can also use `uc_gui_click_captcha()` as a generic CAPTCHA-clicker, which auto-detects between CF Turnstile and Google reCAPTCHA.
If running on a Linux server, `uc_gui_handle_captcha()` might not be good enough. Switch to `uc_gui_click_captcha()` to be more stealthy. Note that these methods auto-detect between CF Turnstile and Google reCAPTCHA.
👤 Here's an example <b>where the CAPTCHA appears after submitting a form</b>:
@ -118,7 +120,7 @@ with SB(uc=True, test=True, ad_block=True) as sb:
<img src="https://seleniumbase.github.io/other/ttm_bypass.png" title="SeleniumBase" width="540">
👤 <b>On Linux</b>, use `sb.uc_gui_click_cf()` to handle Cloudflare Turnstiles:
👤 <b>On Linux</b>, use `sb.uc_gui_click_captcha()` to handle CAPTCHAs (Cloudflare Turnstiles):
```python
from seleniumbase import SB
@ -127,7 +129,7 @@ with SB(uc=True, test=True) as sb:
url = "https://www.virtualmanager.com/en/login"
sb.uc_open_with_reconnect(url, 4)
print(sb.get_page_title())
sb.uc_gui_click_cf() # Ready if needed!
sb.uc_gui_click_captcha() # Only if needed
print(sb.get_page_title())
sb.assert_element('input[name*="email"]')
sb.assert_element('input[name*="login"]')
@ -135,7 +137,7 @@ with SB(uc=True, test=True) as sb:
sb.post_message("SeleniumBase wasn't detected!")
```
<a href="https://github.com/mdmintz/undetected-testing/actions/runs/9637461606/job/26576722411"><img width="540" alt="uc_gui_click_cf on Linux" src="https://github.com/seleniumbase/SeleniumBase/assets/6788579/6aceb2a3-2a32-4521-b30a-f79446d2ce28"></a>
<a href="https://github.com/mdmintz/undetected-testing/actions/runs/9637461606/job/26576722411"><img width="540" alt="uc_gui_click_captcha on Linux" src="https://github.com/seleniumbase/SeleniumBase/assets/6788579/6aceb2a3-2a32-4521-b30a-f79446d2ce28"></a>
The 2nd `print()` should output "Virtual Manager", which means that the automation successfully passed the Turnstile.
@ -191,12 +193,12 @@ driver.uc_gui_write(text)
driver.uc_gui_click_x_y(x, y, timeframe=0.25)
driver.uc_gui_click_captcha(frame="iframe", retry=False, blind=False)
# driver.uc_gui_click_cf(frame="iframe", retry=False, blind=False)
# driver.uc_gui_click_rc(frame="iframe", retry=False, blind=False)
driver.uc_gui_click_rc(frame="iframe", retry=False, blind=False)
driver.uc_gui_click_cf(frame="iframe", retry=False, blind=False)
driver.uc_gui_handle_cf(frame="iframe")
driver.uc_gui_handle_captcha(frame="iframe")
# driver.uc_gui_handle_cf(frame="iframe")
# driver.uc_gui_handle_rc(frame="iframe")
driver.uc_switch_to_frame(frame, reconnect_time=None)
```
@ -233,14 +235,14 @@ driver.reconnect("breakpoint")
(Note that while the special <b><code translate="no">UC Mode</code></b> breakpoint is active, you can't use <b><code translate="no">Selenium</code></b> commands in the browser, and the browser can't detect <b><code translate="no">Selenium</code></b>.)
👤 On Linux, you may need to use `driver.uc_gui_click_cf()` to successfully bypass a Cloudflare CAPTCHA. If there's more than one Cloudflare iframe on that website, then put the CSS Selector of an element that's above the iframe as the first arg to `driver.uc_gui_click_cf()`. This method uses `pyautogui`. In order for `pyautogui` to focus on the correct element, use `xvfb=True` / `--xvfb` to activate a special virtual display on Linux.
👤 On Linux, you may need to use `driver.uc_gui_click_captcha()` to successfully bypass a Cloudflare CAPTCHA. If there's more than one Cloudflare iframe on that website, then put the CSS Selector of an element that's above the iframe as the first arg to `driver.uc_gui_click_captcha()`. This method uses `pyautogui`. In order for `pyautogui` to focus on the correct element, use `xvfb=True` / `--xvfb` to activate a special virtual display on Linux.
👤 `driver.uc_gui_click_captcha()` auto-detects the CAPTCHA type before trying to click it. This is a generic method for both CF Turnstile and Google reCAPTCHA. It will use the code from `uc_gui_click_cf()` and `uc_gui_click_rc()` as needed.
👤 `driver.uc_gui_click_cf(frame="iframe", retry=False, blind=False)` has three args. (All optional). The first one, `frame`, lets you specify the iframe in case the CAPTCHA is not located in the first iframe on the page. The second one, `retry`, lets you retry the click after reloading the page if the first one didn't work (and a CAPTCHA is still present after the page reload). The third arg, `blind`, will retry after a page reload (if the first click failed) by clicking at the last known coordinates of the CAPTCHA checkbox without confirming first with Selenium that a CAPTCHA is still on the page.
👤 `driver.uc_gui_click_rc(frame="iframe", retry=False, blind=False)` is for reCAPTCHA. This may only work a few times before not working anymore... not because Selenium was detected, but because reCAPTCHA uses advanced AI to detect unusual activity, unlike the CF Turnstile, which only uses basic detection.
👤 `driver.uc_gui_click_captcha()` auto-detects the CAPTCHA type before trying to click it. This is a generic method for both CF Turnstile and Google reCAPTCHA. It will use the code from `uc_gui_click_cf()` and `uc_gui_click_rc()` as needed.
👤 To find out if <b translate="no">UC Mode</b> will work at all on a specific site (before adjusting for timing), load your site with the following script:
```python
@ -334,11 +336,11 @@ The above JS method is used within the <b><code translate="no">SeleniumBase</cod
🛠️ <b>Troubleshooting UC Mode</b>
On Windows, the `uc_gui_click_cf()` and `uc_gui_click_captcha()` methods require "Scaling" to be set at "100%". (Note that "100%" may be different from the system's "Recommended" percent, which can be higher depending on your screen resolution and monitor size.)
On Windows, the `uc_gui_click_captcha()` method requires "Scaling" to be set at "100%". (Note that "100%" may be different from the system's "Recommended" percent, which can be higher depending on your screen resolution and monitor size.)
<img src="https://seleniumbase.github.io/other/ts_uc_1.jpg" title="Make sure Scaling is set to 100%" width="410">
As an alternative to using the `uc_gui_click_cf()` or `uc_gui_click_captcha()` methods on Windows, you can use `sb.uc_gui_handle_cf()`, which does not require "Scaling" to be set to a specific value. Instead of using the mouse to click a CAPTCHA, `sb.uc_gui_handle_cf()` uses a combination of the `TAB` key and the `SPACEBAR`.
As an alternative to using the `uc_gui_click_captcha()` method on Windows, you can use `sb.uc_gui_handle_captcha()`, which does not require "Scaling" to be set to a specific value. Instead of using the mouse to click a CAPTCHA, `sb.uc_gui_handle_captcha()` uses a combination of the `TAB` key and the `SPACEBAR`.
--------

View File

@ -20,7 +20,7 @@ lxml==5.2.2
pyquery==2.0.0
readtime==3.0.0
mkdocs==1.6.0
mkdocs-material==9.5.30
mkdocs-material==9.5.31
mkdocs-exclude-search==0.6.6
mkdocs-simple-hooks==0.1.5
mkdocs-material-extensions==1.3.1

View File

@ -1,2 +1,2 @@
# seleniumbase package
__version__ = "4.29.4"
__version__ = "4.29.5"

View File

@ -394,9 +394,9 @@ def uc_special_open_if_cf(
)
uc_metrics = {}
if (
isinstance(device_width, int)
and isinstance(device_height, int)
and isinstance(device_pixel_ratio, int)
isinstance(device_width, (int, float))
and isinstance(device_height, (int, float))
and isinstance(device_pixel_ratio, (int, float))
):
uc_metrics["width"] = device_width
uc_metrics["height"] = device_height
@ -975,9 +975,24 @@ def uc_gui_click_cf(driver, frame="iframe", retry=False, blind=False):
)
def uc_gui_handle_cf(driver, frame="iframe"):
def _uc_gui_handle_captcha(
driver,
frame="iframe",
ctype=None,
):
if ctype == "cf_t":
if not _on_a_cf_turnstile_page(driver):
return
elif ctype == "g_rc":
if not _on_a_g_recaptcha_page(driver):
return
else:
if _on_a_g_recaptcha_page(driver):
ctype = "g_rc"
elif _on_a_cf_turnstile_page(driver):
ctype = "cf_t"
else:
return
install_pyautogui_if_missing(driver)
import pyautogui
pyautogui = get_configured_pyautogui(pyautogui)
@ -988,7 +1003,10 @@ def uc_gui_handle_cf(driver, frame="iframe"):
with gui_lock: # Prevent issues with multiple processes
needs_switch = False
is_in_frame = js_utils.is_in_frame(driver)
if is_in_frame and driver.is_element_present("#challenge-stage"):
selector = "#challenge-stage"
if ctype == "g_rc":
selector = "#recaptcha-token"
if is_in_frame and driver.is_element_present(selector):
driver.switch_to.parent_frame()
needs_switch = True
is_in_frame = js_utils.is_in_frame(driver)
@ -997,6 +1015,7 @@ def uc_gui_handle_cf(driver, frame="iframe"):
page_actions.switch_to_window(
driver, driver.current_window_handle, 2, uc_lock=False
)
if ctype == "cf_t":
if (
driver.is_element_present(".cf-turnstile-wrapper iframe")
or driver.is_element_present(
@ -1039,25 +1058,34 @@ def uc_gui_handle_cf(driver, frame="iframe"):
frame = 'div > div > [style*="margin"][style*="padding"]'
else:
return
else:
if (
driver.is_element_present('iframe[title="reCAPTCHA"]')
and frame == "iframe"
):
frame = 'iframe[title="reCAPTCHA"]'
if not is_in_frame or needs_switch:
# Currently not in frame (or nested frame outside CF one)
try:
if visible_iframe:
if visible_iframe or ctype == "g_rc":
driver.switch_to_frame(frame)
except Exception:
if visible_iframe:
if visible_iframe or ctype == "g_rc":
if driver.is_element_present("iframe"):
driver.switch_to_frame("iframe")
else:
return
try:
selector = "div.cf-turnstile"
if ctype == "g_rc":
selector = "span#recaptcha-anchor"
found_checkbox = False
for i in range(24):
pyautogui.press("\t")
time.sleep(0.02)
active_element_css = js_utils.get_active_element_css(driver)
if (
active_element_css.startswith("div.cf-turnstile")
active_element_css.startswith(selector)
or active_element_css.endswith(" > div" * 2)
):
found_checkbox = True
@ -1081,6 +1109,18 @@ def uc_gui_handle_cf(driver, frame="iframe"):
driver.reconnect(reconnect_time)
def uc_gui_handle_captcha(driver, frame="iframe"):
_uc_gui_handle_captcha(driver, frame=frame, ctype=None)
def uc_gui_handle_cf(driver, frame="iframe"):
_uc_gui_handle_captcha(driver, frame=frame, ctype="cf_t")
def uc_gui_handle_rc(driver, frame="iframe"):
_uc_gui_handle_captcha(driver, frame=frame, ctype="g_rc")
def uc_switch_to_frame(driver, frame="iframe", reconnect_time=None):
from selenium.webdriver.remote.webelement import WebElement
if isinstance(frame, WebElement):
@ -1479,9 +1519,9 @@ def _set_chrome_options(
emulator_settings = {}
device_metrics = {}
if (
isinstance(device_width, int)
and isinstance(device_height, int)
and isinstance(device_pixel_ratio, int)
isinstance(device_width, (int, float))
and isinstance(device_height, (int, float))
and isinstance(device_pixel_ratio, (int, float))
):
device_metrics["width"] = device_width
device_metrics["height"] = device_height
@ -3229,9 +3269,9 @@ def get_local_driver(
emulator_settings = {}
device_metrics = {}
if (
isinstance(device_width, int)
and isinstance(device_height, int)
and isinstance(device_pixel_ratio, int)
isinstance(device_width, (int, float))
and isinstance(device_height, (int, float))
and isinstance(device_pixel_ratio, (int, float))
):
device_metrics["width"] = device_width
device_metrics["height"] = device_height
@ -4421,21 +4461,31 @@ def get_local_driver(
driver, *args, **kwargs
)
)
driver.uc_gui_click_rc = (
lambda *args, **kwargs: uc_gui_click_rc(
driver, *args, **kwargs
)
)
driver.uc_gui_click_cf = (
lambda *args, **kwargs: uc_gui_click_cf(
driver, *args, **kwargs
)
)
driver.uc_gui_click_rc = (
lambda *args, **kwargs: uc_gui_click_rc(
driver, *args, **kwargs
)
)
driver.uc_gui_handle_captcha = (
lambda *args, **kwargs: uc_gui_handle_captcha(
driver, *args, **kwargs
)
)
driver.uc_gui_handle_cf = (
lambda *args, **kwargs: uc_gui_handle_cf(
driver, *args, **kwargs
)
)
driver.uc_gui_handle_rc = (
lambda *args, **kwargs: uc_gui_handle_rc(
driver, *args, **kwargs
)
)
driver.uc_switch_to_frame = (
lambda *args, **kwargs: uc_switch_to_frame(
driver, *args, **kwargs
@ -4446,9 +4496,9 @@ def get_local_driver(
if mobile_emulator:
uc_metrics = {}
if (
isinstance(device_width, int)
and isinstance(device_height, int)
and isinstance(device_pixel_ratio, int)
isinstance(device_width, (int, float))
and isinstance(device_height, (int, float))
and isinstance(device_pixel_ratio, (int, float))
):
uc_metrics["width"] = device_width
uc_metrics["height"] = device_height

View File

@ -4246,12 +4246,16 @@ class BaseCase(unittest.TestCase):
self.uc_gui_click_x_y = new_driver.uc_gui_click_x_y
if hasattr(new_driver, "uc_gui_click_captcha"):
self.uc_gui_click_captcha = new_driver.uc_gui_click_captcha
if hasattr(new_driver, "uc_gui_click_rc"):
self.uc_gui_click_rc = new_driver.uc_gui_click_rc
if hasattr(new_driver, "uc_gui_click_cf"):
self.uc_gui_click_cf = new_driver.uc_gui_click_cf
if hasattr(new_driver, "uc_gui_click_rc"):
self.uc_gui_click_rc = new_driver.uc_gui_click_rc
if hasattr(new_driver, "uc_gui_handle_captcha"):
self.uc_gui_handle_captcha = new_driver.uc_gui_handle_captcha
if hasattr(new_driver, "uc_gui_handle_cf"):
self.uc_gui_handle_cf = new_driver.uc_gui_handle_cf
if hasattr(new_driver, "uc_gui_handle_rc"):
self.uc_gui_handle_rc = new_driver.uc_gui_handle_rc
if hasattr(new_driver, "uc_switch_to_frame"):
self.uc_switch_to_frame = new_driver.uc_switch_to_frame
return new_driver