You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
👤 SeleniumBase <b>UC Mode</b> (Undetected-Chromedriver Mode) allows bots to appear human, which lets them evade detection from anti-bot services that try to block them or trigger CAPTCHAs on various websites.
5
+
👤 <btranslate="no">SeleniumBase</b> <btranslate="no">UC Mode</b> (Undetected-Chromedriver Mode) allows bots to appear human, which lets them evade detection from anti-bot services that try to block them or trigger CAPTCHAs on various websites.
6
6
7
-
<!-- YouTube View --><ahref="https://www.youtube.com/watch?v=5dMFI3e85ig"><imgsrc="http://img.youtube.com/vi/5dMFI3e85ig/0.jpg"title="SeleniumBase on YouTube"width="335" /></a>
8
-
<!-- GitHub Only --><p>(<b><ahref="https://www.youtube.com/watch?v=5dMFI3e85ig">Watch the UC Mode tutorial on YouTube</a></b>)</p>
7
+
<!-- YouTube View --><ahref="https://www.youtube.com/watch?v=5dMFI3e85ig"><imgsrc="http://img.youtube.com/vi/5dMFI3e85ig/0.jpg"title="SeleniumBase on YouTube"width="400" /></a>
8
+
<!-- GitHub Only --><p>(<b><ahref="https://www.youtube.com/watch?v=5dMFI3e85ig">Watch the UC Mode tutorial on YouTube! ▶️</a></b>)</p>
9
9
10
-
👤 <b>UC Mode</b> is based on [undetected-chromedriver](https://github.com/ultrafunkamsterdam/undetected-chromedriver), but includes multiple updates, fixes, and improvements to support a wider range of features and edge cases:
10
+
👤 <btranslate="no">UC Mode</b> is based on [undetected-chromedriver](https://github.com/ultrafunkamsterdam/undetected-chromedriver), but includes multiple updates, fixes, and improvements to support a wider range of features and edge cases:
11
11
12
-
* Includes driver version-detection & management.
13
-
* Allows mismatched browser/driver versions.
14
-
* Automatically changes the user agent to prevent detection. (`HeadlessChrome` to `Chrome`)
15
-
* Automatically disconnects chromedriver from Chrome as needed. (And reconnects)
12
+
* Automatically changes the user agent to prevent detection.
16
13
* Supports multithreaded tests in parallel via `pytest-xdist`.
17
-
* Adjusts configuration based on the environment. (Linux/Ubuntu vs Windows vs macOS)
14
+
* Adjusts some configuration based on the environment.
15
+
* Includes driver version-detection and management.
18
16
* Has options for setting proxy and proxy-with-auth.
19
-
* Has ways of adjusting timings from default values.
17
+
* Has args for adjusting timings from default values.
20
18
* Includes multiple ways of structuring test scripts.
21
19
22
-
👤 Here's an example with the `Driver` manager:
20
+
👤 Here's an example with the <b><codetranslate="no">Driver</code></b> manager:
👤 Here's an example with the `SB` manager: (Has more methods than the `Driver` format, and also quits the driver automatically after the `with` block ends.)
30
+
👤 Here's an example with the <b><codetranslate="no">SB</code></b> manager (which has more methods and functionality than the <b><codetranslate="no">Driver</code></b> format):
33
31
34
32
```python
35
33
from seleniumbase importSB
@@ -53,7 +51,9 @@ with SB(uc=True, test=True) as sb:
### 👤 Here are the <b><codetranslate="no">driver</code></b>-specific methods added by SeleniumBase for UC Mode: `--uc` / <b><codetranslate="no">uc=True</code></b>
(Note that the `reconnect_time` is used to specify how long the driver should be disconnected from Chrome to prevent detection before reconnecting again.)
123
+
(Note that the <b><codetranslate="no">reconnect_time</code></b> is used to specify how long the driver should be disconnected from Chrome to prevent detection before reconnecting again.)
114
124
115
-
👤 Since `driver.get(url)` is slower in UC Mode for bypassing detection, use `driver.default_get(url)` for a standard page load instead:
125
+
👤 Since <b><codetranslate="no">driver.get(url)</code></b> is slower in UC Mode for bypassing detection, use <b><codetranslate="no">driver.default_get(url)</code></b> for a standard page load instead:
116
126
117
127
```python
118
128
driver.default_get(url) # Faster, but Selenium can be detected
119
129
```
120
130
121
-
👤 Here are some examples of using those special UC Mode methods: (Use `self.driver` for `BaseCase` formats. Use `sb.driver` for `SB()` formats):
131
+
👤 Here are some examples of using those special <btranslate="no">UC Mode</b> methods: (Use <b><codetranslate="no">self.driver</code></b> for <b><codetranslate="no">BaseCase</code></b> formats. Use <b><codetranslate="no">sb.driver</code></b> for <b><codetranslate="no">SB()</code></b> formats):
122
132
123
133
```python
124
134
url ="https://gitlab.com/users/sign_in"
@@ -129,7 +139,7 @@ driver.reconnect(5)
129
139
driver.reconnect(timeout=5)
130
140
```
131
141
132
-
👤 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):
142
+
👤 You can also set the <b><codetranslate="no">reconnect_time</code></b> / <b><codetranslate="no">timeout</code></b> to <b><codetranslate="no">"breakpoint"</code></b> as a valid option. This allows the user to perform manual actions (until typing <b><codetranslate="no">c</code></b> and pressing <b><codetranslate="no">ENTER</code></b> to continue from the breakpoint):
(Note that while the special UC Mode breakpoint is active, you can't issue Selenium commands to the browser, and the browser can't detect Selenium.)
153
+
(Note that while the special <b><codetranslate="no">UC Mode</code></b> breakpoint is active, you can't use <b><codetranslate="no">Selenium</code></b> commands in the browser, and the browser can't detect <b><codetranslate="no">Selenium</code></b>.)
154
+
155
+
👤 The two main causes of getting detected in <btranslate="no">UC Mode</b> (which are both easily handled) are:
144
156
145
-
👤 The two main causes of getting detected in UC Mode (which are both easily handled) are:
146
-
* Timing. (UC Mode methods let you customize default values that aren't good enough for your environment.)
147
-
* Not using `driver.uc_click(selector)` when you need to remain undetected while clicking something.
157
+
<li>Timing. (<btranslate="no">UC Mode</b> methods let you customize default values that aren't good enough for your environment.)</li>
158
+
<li>Not using <b><codetranslate="no">driver.uc_click(selector)</code></b> when you need to remain undetected while clicking something.</li>
148
159
149
-
👤 To find out if UC Mode will work at all on a specific site (before adjusting for timing), load your site with the following script:
160
+
👤 To find out if <btranslate="no">UC Mode</b> will work at all on a specific site (before adjusting for timing), load your site with the following script:
(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.)
169
+
(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 <b><codetranslate="no">driver.uc_click</code></b> as needed.)
159
170
160
171
👤 <b>Multithreaded UC Mode:</b>
161
172
162
-
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:
173
+
If you're using <b><codetranslate="no">pytest</code></b> for multithreaded <btranslate="no">UC Mode</b> (which requires using one of the <b><codetranslate="no">pytest</code></b>[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:
163
174
164
175
```bash
165
176
pytest --uc -n 4
166
177
```
167
178
168
-
(Then `pytest-xdist` is automatically used to spin up and process the threads.)
179
+
(Then <b><codetranslate="no">pytest-xdist</code></b> is automatically used to spin up and process the threads.)
169
180
170
-
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.
181
+
If you don't want to use <b><codetranslate="no">pytest</code></b> for multithreading, then you'll need to do a little more work. That involves using a different multithreading library, (eg. <b><codetranslate="no">concurrent.futures</code></b>), and making sure that thread-locking is done correctly for processes that share resources. To handle that thread-locking, include <b><codetranslate="no">sys.argv.append("-n")</code></b> in your <b>SeleniumBase</b> file.
171
182
172
-
Here's a sample script that uses `concurrent.futures` for spinning up multiple processes:
183
+
Here's a sample script that uses <b><codetranslate="no">concurrent.futures</code></b> for spinning up multiple processes:
173
184
174
185
```python
175
186
import sys
@@ -190,3 +201,89 @@ with ThreadPoolExecutor(max_workers=len(urls)) as executor:
190
201
for url in urls:
191
202
executor.submit(launch_driver, url)
192
203
```
204
+
205
+
--------
206
+
207
+
👥 <b>Double Duty:</b> Here's an example of handling two CAPTCHAs on one page:
Here are the 3 primary things that <btranslate="no">UC Mode</b> does to make bots appear human:
239
+
240
+
<li>Modifies <b><codetranslate="no">chromedriver</code></b> to rename <btranslate="no">Chrome DevTools Console</b> variables.</li>
241
+
<li>Launches <btranslate="no">Chrome</b> browsers before attaching <b><codetranslate="no">chromedriver</code></b> to them.</li>
242
+
<li>Disconnects <b><codetranslate="no">chromedriver</code></b> from <btranslate="no">Chrome</b> during stealthy actions.</li>
243
+
244
+
For example, if the <btranslate="no">Chrome DevTools Console</b> variables aren't renamed, you can expect to find them easily when using <b><codetranslate="no">selenium</code></b> for browser automation:
(If those variables are still there, then websites can easily detect your bots.)
249
+
250
+
If you launch <btranslate="no">Chrome</b> using <b><codetranslate="no">chromedriver</code></b>, then there will be settings that make your browser look like a bot. (Instead, <btranslate="no">UC Mode</b> connects <b><codetranslate="no">chromedriver</code></b> to <btranslate="no">Chrome</b> after the browser is launched, which makes <btranslate="no">Chrome</b> look like a normal, human-controlled web browser.)
251
+
252
+
While <b><codetranslate="no">chromedriver</code></b> is connected to <btranslate="no">Chrome</b>, website services can detect it. Thankfully, raw <b><codetranslate="no">selenium</code></b> already includes <b><codetranslate="no">driver.service.stop()</code></b> for stopping the <b><codetranslate="no">chromedriver</code></b> service, <b><codetranslate="no">driver.service.start()</code></b> for starting the <b><codetranslate="no">chromedriver</code></b> service, and <b><codetranslate="no">driver.start_session(capabilities)</code></b> for reviving the active browser session with the given capabilities. (<btranslate="no"><code>SeleniumBase</code> UC Mode</b> methods automatically use those raw <b><codetranslate="no">selenium</code></b> methods as needed.)
253
+
254
+
Links to those <ahref="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):
Also note that <b><codetranslate="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):
The above JS method is used within <btranslate="no"><code>SeleniumBase</code></b> <btranslate="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), <btranslate="no">UC Mode</b> tells <b><codetranslate="no">chromedriver</code></b> to connect to that tab so that automated commands can now be issued. At that point, <b><codetranslate="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).
265
+
266
+
Avoiding detection while clicking is easy if you schedule your clicks to happen at a future point when the <b><codetranslate="no">chromedriver</code></b> service has been stopped. Here's a JS command that lets you schedule events (such as clicks) to happen in the future:
The above JS method is used within the <b><codetranslate="no">SeleniumBase</code></b> <btranslate="no">UC Mode</b> method: <b><codetranslate="no">driver.uc_click(selector)</code></b> so that clicking can be done in a stealthy way. <btranslate="no">UC Mode</b> schedules your click, disconnects <b><codetranslate="no">chromedriver</code></b> from <btranslate="no">Chrome</b>, waits a little (customizable), and reconnects.
272
+
273
+
--------
274
+
275
+
🏆 <b>Choosing the right CAPTCHA service</b> for your business / website:
As an ethical hacker / cybersecurity researcher who builds bots that bypass CAPTCHAs for sport, <b>the CAPTCHA service that I personally recommend</b> for keeping bots out is <btranslate="no">Google's reCAPTCHA</b>:
Since Google makes Chrome, Google's own <btranslate="no">reCAPTCHA</b> service has access to more data than other CAPTCHA services (eg. hCaptcha, CloudFlare, DataDome, etc.), and can therefore use that data to make better decisions about whether or not web activity is coming from real humans or automated bots.
0 commit comments