Skip to content

Commit 40dd528

Browse files
committed
Update the CDP Mode docs
1 parent dd6659b commit 40dd528

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

examples/cdp_mode/ReadMe.md

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
👤 <b translate="no">UC Mode</b> avoids bot-detection by first disconnecting WebDriver from the browser at strategic times, calling special <code>PyAutoGUI</code> methods to bypass CAPTCHAs (as needed), and finally reconnecting the <code>driver</code> afterwards so that WebDriver actions can be performed again. Although this approach works for bypassing simple CAPTCHAs, more flexibility is needed for bypassing bot-detection on websites with advanced protection. (That's where <b translate="no">CDP Mode</b> comes in.)
88

9-
🐙 <b translate="no">CDP Mode</b> is based on <a href="https://github.com/HyperionGray/python-chrome-devtools-protocol" translate="no">python-cdp</a>, <a href="https://github.com/HyperionGray/trio-chrome-devtools-protocol" translate="no">trio-cdp</a>, and <a href="https://github.com/ultrafunkamsterdam/nodriver" translate="no">nodriver</a>. <code>trio-cdp</code> was an early implementation of <code>python-cdp</code>, whereas <code>nodriver</code> is a modern implementation of <code>python-cdp</code>. (Refactored CDP code is imported from <a href="https://github.com/mdmintz/MyCDP" translate="no">MyCDP</a>.)
9+
🐙 <b translate="no">CDP Mode</b> is based on <a href="https://github.com/HyperionGray/python-chrome-devtools-protocol" translate="no">python-cdp</a>, <a href="https://github.com/HyperionGray/trio-chrome-devtools-protocol" translate="no">trio-cdp</a>, and <a href="https://github.com/ultrafunkamsterdam/nodriver" translate="no">nodriver</a>. <code>trio-cdp</code> is an early implementation of <code>python-cdp</code>, and <code>nodriver</code> is a modern implementation of <code>python-cdp</code>. (Refactored Python-CDP code is imported from <a href="https://github.com/mdmintz/MyCDP" translate="no">MyCDP</a>.)
1010

1111
🐙 <b translate="no">CDP Mode</b> includes multiple updates to the above, such as:
1212

@@ -19,12 +19,41 @@
1919

2020
--------
2121

22-
### 🐙 <b translate="no">CDP Mode</b> initialization:
22+
### 🐙 <b translate="no">CDP Mode</b> usage:
2323

24-
* `sb.activate_cdp_mode(url)`
24+
* **`sb.activate_cdp_mode(url)`**
2525

2626
> (Call that from a **UC Mode** script)
2727
28+
That disconnects WebDriver from Chrome (which prevents detection), and gives you access to `sb.cdp` methods (which don't trigger anti-bot checks).
29+
30+
### 🐙 Here are some common `sb.cdp` methods:
31+
32+
* `sb.cdp.click(selector)`
33+
* `sb.cdp.click_if_visible(selector)`
34+
* `sb.cdp.type(selector, text)`
35+
* `sb.cdp.press_keys(selector, text)`
36+
* `sb.cdp.select_all(selector)`
37+
* `sb.cdp.get_text(selector)`
38+
39+
When `type()` is too fast, use the slower `press_keys()` to avoid detection. You can also use `sb.sleep(seconds)` to slow things down.
40+
41+
To use WebDriver methods again, call:
42+
43+
* **`sb.reconnect()`** or **`sb.connect()`**
44+
45+
(Note that reconnecting allows anti-bots to detect you, so only reconnect if it is safe to do so.)
46+
47+
To disconnect again, call:
48+
49+
* **`sb.disconnect()`**
50+
51+
While disconnected, if you accidentally call a WebDriver method, then SeleniumBase will attempt to use the CDP Mode version of that method (if available). For example, if you accidentally call `sb.click(selector)` instead of `sb.cdp.click(selector)`, then your WebDriver call will automatically be redirected to the CDP Mode version. Not all WebDriver methods have a matching CDP Mode method. In that scenario, calling a WebDriver method while disconnected could raise an error, or make WebDriver automatically reconnect first.
52+
53+
To find out if WebDriver is connected or disconnected, call:
54+
55+
* **`sb.is_connected()`**
56+
2857
--------
2958

3059
### 🐙 <b translate="no">CDP Mode</b> examples:
@@ -45,13 +74,15 @@ from seleniumbase import SB
4574
with SB(uc=True, test=True, locale_code="en") as sb:
4675
url = "https://www.pokemon.com/us"
4776
sb.activate_cdp_mode(url)
48-
sb.sleep(1)
77+
sb.sleep(1.5)
4978
sb.cdp.click_if_visible("button#onetrust-reject-all-handler")
79+
sb.sleep(0.5)
5080
sb.cdp.click('a[href="https://www.pokemon.com/us/pokedex/"]')
5181
sb.sleep(1)
5282
sb.cdp.click('b:contains("Show Advanced Search")')
5383
sb.sleep(1)
5484
sb.cdp.click('span[data-type="type"][data-value="electric"]')
85+
sb.sleep(0.5)
5586
sb.cdp.click("a#advSearch")
5687
sb.sleep(1)
5788
sb.cdp.click('img[src*="img/pokedex/detail/025.png"]')
@@ -99,7 +130,7 @@ from seleniumbase import SB
99130
with SB(uc=True, test=True, locale_code="en") as sb:
100131
url = "https://www.hyatt.com/"
101132
sb.activate_cdp_mode(url)
102-
sb.sleep(1)
133+
sb.sleep(1.5)
103134
sb.cdp.click_if_visible('button[aria-label="Close"]')
104135
sb.sleep(0.5)
105136
sb.cdp.click('span:contains("Explore")')
@@ -188,10 +219,14 @@ with SB(uc=True, test=True, locale_code="en") as sb:
188219

189220
```python
190221
sb.cdp.get(url)
191-
sb.cdp.reload()
222+
sb.cdp.open(url)
223+
sb.cdp.reload(ignore_cache=True, script_to_evaluate_on_load=None)
192224
sb.cdp.refresh()
225+
sb.cdp.get_event_loop()
193226
sb.cdp.add_handler(event, handler)
194227
sb.cdp.find_element(selector)
228+
sb.cdp.find(selector)
229+
sb.cdp.locator(selector)
195230
sb.cdp.find_all(selector)
196231
sb.cdp.find_elements_by_text(text, tag_name=None)
197232
sb.cdp.select(selector)
@@ -205,6 +240,7 @@ sb.cdp.load_cookies(*args, **kwargs)
205240
sb.cdp.clear_cookies(*args, **kwargs)
206241
sb.cdp.sleep(seconds)
207242
sb.cdp.bring_active_window_to_front()
243+
sb.cdp.bring_to_front()
208244
sb.cdp.get_active_element()
209245
sb.cdp.get_active_element_css()
210246
sb.cdp.click(selector)
@@ -231,7 +267,7 @@ sb.cdp.medimize()
231267
sb.cdp.set_window_rect()
232268
sb.cdp.reset_window_size()
233269
sb.cdp.get_window()
234-
sb.cdp.get_text()
270+
sb.cdp.get_text(selector)
235271
sb.cdp.get_title()
236272
sb.cdp.get_current_url()
237273
sb.cdp.get_origin()

0 commit comments

Comments
 (0)