Skip to content

Commit aeedbd7

Browse files
committed
Update UC Mode docs
1 parent 0deefa3 commit aeedbd7

File tree

1 file changed

+50
-14
lines changed

1 file changed

+50
-14
lines changed

help_docs/uc_mode.md

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from seleniumbase import Driver
2626

2727
driver = Driver(uc=True)
28-
driver.uc_open_with_reconnect("https://top.gg/", 6)
28+
driver.uc_open_with_reconnect("https://gitlab.com/users/sign_in", 3)
2929
driver.quit()
3030
```
3131

@@ -35,7 +35,7 @@ driver.quit()
3535
from seleniumbase import SB
3636

3737
with SB(uc=True) as sb:
38-
sb.driver.uc_open_with_reconnect("https://top.gg/", 6)
38+
sb.driver.uc_open_with_reconnect("https://gitlab.com/users/sign_in", 3)
3939
```
4040

4141
👤 Here's a longer example, which includes a retry if the CAPTCHA isn't bypassed on the first attempt:
@@ -44,13 +44,13 @@ with SB(uc=True) as sb:
4444
from seleniumbase import SB
4545

4646
with SB(uc=True, test=True) as sb:
47-
sb.driver.uc_open_with_reconnect("https://top.gg/", 5)
48-
if not sb.is_text_visible("Discord Bots", "h1"):
49-
sb.driver.uc_open_with_reconnect("https://top.gg/", 5)
50-
sb.assert_text("Discord Bots", "h1", timeout=3)
51-
sb.highlight("h1", loops=3)
52-
sb.set_messenger_theme(location="top_center")
53-
sb.post_message("Selenium wasn't detected!", duration=3)
47+
url = "https://gitlab.com/users/sign_in"
48+
sb.driver.uc_open_with_reconnect(url, 3)
49+
if not sb.is_text_visible("Username", '[for="user_login"]'):
50+
sb.driver.uc_open_with_reconnect(url, 4)
51+
sb.assert_text("Username", '[for="user_login"]', timeout=3)
52+
sb.highlight('label[for="user_login"]', loops=3)
53+
sb.post_message("SeleniumBase wasn't detected", duration=4)
5454
```
5555

5656
👤 Here's an example where clicking the checkbox is required, even for humans: (Commonly seen with forms that are CAPTCHA-protected.)
@@ -76,7 +76,7 @@ with SB(uc=True, test=True) as sb:
7676
open_the_turnstile_page(sb)
7777
click_turnstile_and_verify(sb)
7878
sb.set_messenger_theme(location="top_left")
79-
sb.post_message("Selenium wasn't detected!", duration=3)
79+
sb.post_message("SeleniumBase wasn't detected", duration=3)
8080
```
8181

8282
### 👤 Here are some examples that use UC Mode:
@@ -117,8 +117,9 @@ driver.default_get(url) # Faster, but Selenium can be detected
117117
👤 Here are some examples of using those special UC Mode methods: (Use `self.driver` for `BaseCase` formats. Use `sb.driver` for `SB()` formats):
118118

119119
```python
120-
driver.uc_open_with_reconnect("https://top.gg/", reconnect_time=5)
121-
driver.uc_open_with_reconnect("https://top.gg/", 5)
120+
url = "https://gitlab.com/users/sign_in"
121+
driver.uc_open_with_reconnect(url, reconnect_time=3)
122+
driver.uc_open_with_reconnect(url, 3)
122123

123124
driver.reconnect(5)
124125
driver.reconnect(timeout=5)
@@ -127,8 +128,9 @@ driver.reconnect(timeout=5)
127128
👤 You can also set the `reconnect_time` / `timeout` to `"breakpoint"` as a valid option. This allows the user to perform manual actions (until typing `c` and pressing ENTER to continue from the breakpoint):
128129

129130
```python
130-
driver.uc_open_with_reconnect("https://top.gg/", reconnect_time="breakpoint")
131-
driver.uc_open_with_reconnect("https://top.gg/", "breakpoint")
131+
url = "https://gitlab.com/users/sign_in"
132+
driver.uc_open_with_reconnect(url, reconnect_time="breakpoint")
133+
driver.uc_open_with_reconnect(url, "breakpoint")
132134

133135
driver.reconnect(timeout="breakpoint")
134136
driver.reconnect("breakpoint")
@@ -150,3 +152,37 @@ with SB(uc=True) as sb:
150152
```
151153

152154
(If you remain undetected while loading the page and performing manual actions, then you know you can create a working script once you swap the breakpoint with a time, and add special methods like `uc_click` as needed.)
155+
156+
👤 <b>Multithreaded UC Mode:</b>
157+
158+
If you're using `pytest` for multithreaded UC Mode (which requires using one of the `pytest` [syntax formats](https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/syntax_formats.md)), then all you have to do is set the number of threads when your script runs. (`-n NUM`) Eg:
159+
160+
```bash
161+
pytest --uc -n 4
162+
```
163+
164+
(Then `pytest-xdist` is automatically used to spin up and process the threads.)
165+
166+
If you don't want to use `pytest` for multithreading, then you'll need to do a little more work. That involves using a different multithreading library, (eg. `concurrent.futures`), and making sure that thread-locking is done correctly for processes that share resources. To handle that thread-locking, include `sys.argv.append("-n")` in your SeleniumBase file.
167+
168+
Here's a sample script that uses `concurrent.futures` for spinning up multiple processes:
169+
170+
```python
171+
import sys
172+
from concurrent.futures import ThreadPoolExecutor
173+
from seleniumbase import Driver
174+
sys.argv.append("-n") # Tell SeleniumBase to do thread-locking as needed
175+
176+
def launch_driver(url):
177+
driver = Driver(uc=True)
178+
try:
179+
driver.get(url=url)
180+
driver.sleep(2)
181+
finally:
182+
driver.quit()
183+
184+
urls = ['https://seleniumbase.io/demo_page' for i in range(3)]
185+
with ThreadPoolExecutor(max_workers=len(urls)) as executor:
186+
for url in urls:
187+
executor.submit(launch_driver, url)
188+
```

0 commit comments

Comments
 (0)