Skip to content

Commit 12e64a1

Browse files
committed
Update the UC Mode documentation
1 parent f67449a commit 12e64a1

File tree

1 file changed

+32
-53
lines changed

1 file changed

+32
-53
lines changed

help_docs/uc_mode.md

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
* Automatically changing user agents to prevent detection.
2222
* Automatically setting various chromium args as needed.
23-
* Has special methods. Eg. `driver.uc_click(selector)`
23+
* Has special `uc_*()` methods.
2424

2525
👤 Here's an example with the <b><code translate="no">Driver</code></b> manager:
2626

@@ -67,22 +67,11 @@ with SB(uc=True, test=True) as sb:
6767
```python
6868
from seleniumbase import SB
6969

70-
def open_the_turnstile_page(sb):
70+
with SB(uc=True, test=True) as sb:
7171
url = "seleniumbase.io/apps/turnstile"
72-
sb.driver.uc_open_with_reconnect(url, reconnect_time=2)
73-
74-
def click_turnstile_and_verify(sb):
75-
sb.switch_to_frame("iframe")
76-
sb.driver.uc_click("span")
72+
sb.uc_open_with_reconnect(url, reconnect_time=2)
73+
sb.uc_gui_handle_cf()
7774
sb.assert_element("img#captcha-success", timeout=3)
78-
79-
with SB(uc=True, test=True) as sb:
80-
open_the_turnstile_page(sb)
81-
try:
82-
click_turnstile_and_verify(sb)
83-
except Exception:
84-
open_the_turnstile_page(sb)
85-
click_turnstile_and_verify(sb)
8675
sb.set_messenger_theme(location="top_left")
8776
sb.post_message("SeleniumBase wasn't detected", duration=3)
8877
```
@@ -129,6 +118,27 @@ with SB(uc=True, test=True, ad_block_on=True) as sb:
129118

130119
<img src="https://seleniumbase.github.io/other/ttm_bypass.png" title="SeleniumBase" width="540">
131120

121+
👤 <b>On Linux</b>, use `sb.uc_gui_handle_cf()` to handle Cloudflare Turnstiles:
122+
123+
```python
124+
from seleniumbase import SB
125+
126+
with SB(uc=True, test=True) as sb:
127+
url = "https://www.virtualmanager.com/en/login"
128+
sb.uc_open_with_reconnect(url, 4)
129+
print(sb.get_page_title())
130+
sb.uc_gui_handle_cf() # Ready if needed!
131+
print(sb.get_page_title())
132+
sb.assert_element('input[name*="email"]')
133+
sb.assert_element('input[name*="login"]')
134+
sb.set_messenger_theme(location="bottom_center")
135+
sb.post_message("SeleniumBase wasn't detected!")
136+
```
137+
138+
<a href="https://github.com/mdmintz/undetected-testing/actions/runs/9637461606/job/26576722411"><img width="540" alt="uc_gui_handle_cf on Linux" src="https://github.com/seleniumbase/SeleniumBase/assets/6788579/6aceb2a3-2a32-4521-b30a-f79446d2ce28"></a>
139+
140+
The 2nd `print()` should output "Virtual Manager", which means that the automation successfully passed the Turnstile.
141+
132142
--------
133143

134144
👤 In <b translate="no">UC Mode</b>, <code translate="no">driver.get(url)</code> has been modified from its original version: If anti-bot services are detected from a <code translate="no">requests.get(url)</code> call that's made before navigating to the website, then <code translate="no">driver.uc_open_with_reconnect(url)</code> will be used instead. To open a URL normally in <b translate="no">UC Mode</b>, use <code translate="no">driver.default_get(url)</code>.
@@ -144,6 +154,7 @@ with SB(uc=True, test=True, ad_block_on=True) as sb:
144154
<img src="https://seleniumbase.github.io/other/pixelscan.jpg" title="SeleniumBase" width="540">
145155

146156
### 👤 Here are some UC Mode examples that bypass CAPTCHAs when clicking is required:
157+
* [SeleniumBase/examples/raw_pyautogui.py](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_pyautogui.py)
147158
* [SeleniumBase/examples/raw_turnstile.py](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_turnstile.py)
148159
* [SeleniumBase/examples/raw_form_turnstile.py](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_form_turnstile.py)
149160
* [SeleniumBase/examples/uc_cdp_events.py](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/uc_cdp_events.py)
@@ -214,11 +225,6 @@ driver.reconnect("breakpoint")
214225

215226
(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>.)
216227

217-
👤 The two main causes of getting detected in <b translate="no">UC Mode</b> (which are both easily handled) are:
218-
219-
<li>Timing. (<b translate="no">UC Mode</b> methods let you customize default values that aren't good enough for your environment.)</li>
220-
<li>Not using <b><code translate="no">driver.uc_click(selector)</code></b> when you need to remain undetected while clicking something.</li>
221-
222228
👤 On Linux, you may need to use `driver.uc_gui_handle_cf()` to successfully bypass a Cloudflare CAPTCHA. If there's more than one iframe on that website (and Cloudflare isn't the first one) then put the CSS Selector of that iframe as the first arg to `driver.uc_gui_handle_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.
223229

224230
👤 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:
@@ -268,46 +274,15 @@ with ThreadPoolExecutor(max_workers=len(urls)) as executor:
268274

269275
--------
270276

271-
👥 <b>Double Duty:</b> Here's an example of handling two CAPTCHAs on one page:
272-
273-
<img src="https://seleniumbase.github.io/other/nopecha.png" title="SeleniumBase" align="center" width="630">
274-
275-
```python
276-
from seleniumbase import SB
277-
278-
with SB(uc=True, test=True) as sb:
279-
sb.driver.uc_open_with_reconnect("nopecha.com/demo/turnstile", 3.4)
280-
if sb.is_element_visible("#example-container0 iframe"):
281-
sb.switch_to_frame("#example-container0 iframe")
282-
if not sb.is_element_visible("circle.success-circle"):
283-
sb.driver.uc_click("span", reconnect_time=3)
284-
sb.switch_to_frame("#example-container0 iframe")
285-
sb.switch_to_default_content()
286-
287-
sb.switch_to_frame("#example-container5 iframe")
288-
sb.driver.uc_click("span", reconnect_time=2.5)
289-
sb.switch_to_frame("#example-container5 iframe")
290-
sb.assert_element("svg#success-icon", timeout=3)
291-
sb.switch_to_parent_frame()
292-
293-
if sb.is_element_visible("#example-container0 iframe"):
294-
sb.switch_to_frame("#example-container0 iframe")
295-
sb.assert_element("circle.success-circle")
296-
sb.switch_to_parent_frame()
297-
298-
sb.set_messenger_theme(location="top_center")
299-
sb.post_message("SeleniumBase wasn't detected!", duration=3)
300-
```
301-
302-
--------
303-
304277
👤 <b>What makes UC Mode work?</b>
305278

306279
Here are the 3 primary things that <b translate="no">UC Mode</b> does to make bots appear human:
307280

281+
<ul>
308282
<li>Modifies <b><code translate="no">chromedriver</code></b> to rename <b translate="no">Chrome DevTools Console</b> variables.</li>
309283
<li>Launches <b translate="no">Chrome</b> browsers before attaching <b><code translate="no">chromedriver</code></b> to them.</li>
310284
<li>Disconnects <b><code translate="no">chromedriver</code></b> from <b translate="no">Chrome</b> during stealthy actions.</li>
285+
</ul>
311286

312287
For example, if the <b translate="no">Chrome DevTools Console</b> variables aren't renamed, you can expect to find them easily when using <b><code translate="no">selenium</code></b> for browser automation:
313288

@@ -321,13 +296,17 @@ While <b><code translate="no">chromedriver</code></b> is connected to <b transla
321296

322297
Links to those <a href="https://github.com/SeleniumHQ/selenium">raw <b>Selenium</b></a> method definitions have been provided for reference (but you don't need to call those methods directly):
323298

299+
<ul>
324300
<li><b><code translate="no"><a href="https://github.com/SeleniumHQ/selenium/blob/9c6ccdbf40356284fad342f70fbdc0afefd27bd3/py/selenium/webdriver/common/service.py#L135">driver.service.stop()</a></code></b></li>
325301
<li><b><code translate="no"><a href="https://github.com/SeleniumHQ/selenium/blob/9c6ccdbf40356284fad342f70fbdc0afefd27bd3/py/selenium/webdriver/common/service.py#L91">driver.service.start()</a></code></b></li>
326302
<li><b><code translate="no"><a href="https://github.com/SeleniumHQ/selenium/blob/9c6ccdbf40356284fad342f70fbdc0afefd27bd3/py/selenium/webdriver/remote/webdriver.py#L284">driver.start_session(capabilities)</a></code></b></li>
303+
</ul>
327304

328305
Also note that <b><code translate="no">chromedriver</code></b> isn't detectable in a browser tab if it never touches that tab. Here's a JS command that lets you open a URL in a new tab (from your current tab):
329306

307+
<ul>
330308
<li><b><code translate="no">window.open("URL");</code></b> --> (Info: <a href="https://www.w3schools.com/jsref/met_win_open.asp" target="_blank">W3Schools</a>)</li>
309+
</ul>
331310

332311
The above JS method is used within <b translate="no"><code>SeleniumBase</code></b> <b translate="no">UC Mode</b> methods for opening URLs in a stealthy way. Since some websites try to detect if your browser is a bot on the initial page load, this allows you to bypass detection in those situations. After a few seconds (customizable), <b translate="no">UC Mode</b> tells <b><code translate="no">chromedriver</code></b> to connect to that tab so that automated commands can now be issued. At that point, <b><code translate="no">chromedriver</code></b> could be detected if websites are looking for it (but generally websites only look for it during specific events, such as page loads, form submissions, and button clicks).
333312

0 commit comments

Comments
 (0)