Skip to content

Commit a1945ec

Browse files
authored
Merge pull request #4352 from seleniumbase/cdp-mode-patch-107
CDP Mode: Patch 107
2 parents 8d4839e + d253cc0 commit a1945ec

71 files changed

Lines changed: 477 additions & 252 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,40 @@
5050
<br />
5151
</p>
5252

53-
🐙 <a translate="no" href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/ReadMe.md"><b>CDP Mode</b></a> bypasses bot-detection and handles CAPTCHAs by driving the browser directly through the <a href="https://chromedevtools.github.io/devtools-protocol/" translate="no">Chrome DevTools Protocol</a>. Includes <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/playwright/ReadMe.md"><b><span translate="no">Stealthy Playwright Mode</span></b></a>, which extends these advanced anti-detection patches to Playwright scripts.
53+
🐙 <a translate="no" href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/ReadMe.md"><b>CDP Mode</b></a> <b>bypasses bot-detection</b> and handles CAPTCHAs with the <a href="https://chromedevtools.github.io/devtools-protocol/" translate="no">Chrome DevTools Protocol</a>. Includes <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/playwright/ReadMe.md"><b><span translate="no">Stealthy Playwright Mode</span></b></a>, which extends CDP Mode's anti-detection to <b>Playwright</b> scripts. <b><code>sb.solve_captcha()</code></b> handles CAPTCHAs that aren't bypassed automatically.
5454

55-
📚 The [SeleniumBase/examples/](https://github.com/seleniumbase/SeleniumBase/tree/master/examples) folder includes over 100 ready-to-run examples of E2E testing. Examples that start with `test_` or end with `_test.py`/`_tests.py` are specifically designed to run with `pytest`. Other examples run directly with raw `python` (those files generally start with `raw_` to avoid confusion).
55+
<b>Python sync version of SeleniumBase's CDP Mode: (<code>sb_cdp</code>)</b>
56+
57+
```python
58+
from seleniumbase import sb_cdp
59+
60+
sb = sb_cdp.Chrome()
61+
sb.open("https://demo.fingerprint.com/playground")
62+
sb.sleep(3)
63+
sb.driver.quit()
64+
```
65+
66+
<b>Playwright can use SeleniumBase's stealth browser:</b>
67+
68+
```python
69+
from playwright.sync_api import sync_playwright
70+
from seleniumbase import sb_cdp
71+
72+
sb = sb_cdp.Chrome()
73+
endpoint_url = sb.get_endpoint_url()
74+
75+
with sync_playwright() as p:
76+
browser = p.chromium.connect_over_cdp(endpoint_url)
77+
page = browser.contexts[0].pages[0]
78+
page.goto("https://browserscan.net/bot-detection")
79+
80+
sb.sleep(3)
81+
sb.driver.quit()
82+
```
83+
84+
--------
85+
86+
📚 The [SeleniumBase/examples/](https://github.com/seleniumbase/SeleniumBase/tree/master/examples) folder includes over 150 ready-to-run examples of E2E testing. Examples that start with `test_` or end with `_test.py`/`_tests.py` run with `pytest`. Other examples run directly with raw `python` (those generally start with `raw_` to avoid confusion).
5687

5788
🥷 Stealthy CDP Mode examples are located in [./examples/cdp_mode/](https://github.com/seleniumbase/SeleniumBase/tree/master/examples/cdp_mode).
5889

@@ -73,12 +104,12 @@
73104
```python
74105
from seleniumbase import sb_cdp
75106

76-
url = "https://www.browserscan.net/bot-detection"
77-
sb = sb_cdp.Chrome(url, locale="en", ad_block=True)
78-
sb.flash("Test Results", duration=3, pause=1)
107+
sb = sb_cdp.Chrome(locale="en", ad_block=True)
108+
sb.open("https://browserscan.net/bot-detection")
109+
sb.flash("Test Results", duration=1.5, pause=0.5)
79110
sb.assert_element('strong:contains("Normal")')
80111
print("Bot Not Detected")
81-
sb.flash('strong:contains("Normal")', duration=3, pause=2)
112+
sb.flash('strong:contains("Normal")', pause=1)
82113
```
83114

84115
<img src="https://seleniumbase.github.io/other/b_scan_results.jpg" width="628" alt="Stealthy architecture flowchart" />
@@ -95,15 +126,18 @@ endpoint_url = sb.get_endpoint_url()
95126
with sync_playwright() as p:
96127
browser = p.chromium.connect_over_cdp(endpoint_url)
97128
page = browser.contexts[0].pages[0]
98-
page.goto("https://www.browserscan.net/bot-detection")
129+
page.goto("https://browserscan.net/bot-detection")
99130
page.wait_for_timeout(500)
100-
sb.flash("Test Results", duration=3, pause=1)
131+
sb.flash("Test Results", duration=1.5, pause=0.5)
101132
sb.assert_element('strong:contains("Normal")')
102-
sb.flash('strong:contains("Normal")', duration=3, pause=2)
133+
print("Bot Not Detected")
134+
sb.flash('strong:contains("Normal")', pause=1)
103135
```
104136

105137
--------
106138

139+
<h3 align="left">🌐 CLI Options for Supported Chromium Browsers</h3>
140+
107141
💡 You can set which Chromium browser to use via command-line options:
108142

109143
```zsh

examples/cdp_mode/ReadMe.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,14 @@ with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
154154
sb.assert_element('img[alt="Pikachu"]')
155155
sb.scroll_into_view("div.pokemon-ability-info")
156156
sb.sleep(1.2)
157-
sb.cdp.flash('div[class*="title"]')
158-
sb.cdp.flash('img[alt="Pikachu"]')
159-
sb.cdp.flash("div.pokemon-ability-info")
157+
sb.flash('div[class*="title"]')
158+
sb.flash('img[alt="Pikachu"]')
159+
sb.flash("div.pokemon-ability-info")
160160
name = sb.get_text("label.styled-select")
161161
info = sb.get_text("div.version-descriptions p.active")
162162
print("*** %s: ***\n* %s" % (name, info))
163163
sb.sleep(2)
164-
sb.cdp.highlight_overlay("div.pokemon-ability-info")
164+
sb.highlight_overlay("div.pokemon-ability-info")
165165
sb.sleep(2)
166166
sb.open("https://events.pokemon.com/EventLocator/")
167167
sb.sleep(2)
@@ -186,10 +186,10 @@ with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
186186
```python
187187
from seleniumbase import SB
188188

189-
with SB(uc=True, test=True, locale="en") as sb:
189+
with SB(uc=True, test=True, locale="en", guest=True) as sb:
190190
url = "https://www.hyatt.com/"
191191
sb.activate_cdp_mode(url)
192-
sb.sleep(3.2)
192+
sb.sleep(3.4)
193193
sb.click_if_visible('button[aria-label="Close"]')
194194
sb.sleep(0.1)
195195
sb.click_if_visible("#onetrust-reject-all-handler")
@@ -200,11 +200,11 @@ with SB(uc=True, test=True, locale="en") as sb:
200200
sb.click('li[data-js="suggestion"]')
201201
sb.sleep(0.6)
202202
sb.click_if_visible('button[aria-label="Close"]')
203-
sb.sleep(0.6)
203+
sb.sleep(0.8)
204204
sb.click("button.be-button-shop")
205205
sb.sleep(1)
206206
sb.click_if_visible('[label="Find Hotels"]')
207-
sb.sleep(5)
207+
sb.sleep(5.5)
208208
card_info = 'div[data-booking-status="BOOKABLE"] [class*="HotelCard_info"]'
209209
hotels = sb.select_all(card_info)
210210
print("Hyatt Hotels in %s:" % location)
@@ -285,7 +285,7 @@ with SB(uc=True, test=True, ad_block=True) as sb:
285285
sb.sleep(1.8)
286286
continue_button = 'button:contains("Continue shopping")'
287287
if sb.is_element_visible(continue_button):
288-
sb.cdp.gui_click_element(continue_button)
288+
sb.gui_click_element(continue_button)
289289
sb.sleep(0.6)
290290
sb.click('input[aria-label="Search"]')
291291
sb.sleep(1.2)
@@ -294,10 +294,10 @@ with SB(uc=True, test=True, ad_block=True) as sb:
294294
sb.press_keys('input[aria-label="Search"]', search + "\n")
295295
sb.sleep(3.8)
296296
if sb.is_element_visible("#px-captcha"):
297-
sb.cdp.gui_click_and_hold("#px-captcha", 7.2)
297+
sb.gui_click_and_hold("#px-captcha", 7.2)
298298
sb.sleep(4.2)
299299
if sb.is_element_visible("#px-captcha"):
300-
sb.cdp.gui_click_and_hold("#px-captcha", 4.2)
300+
sb.gui_click_and_hold("#px-captcha", 4.2)
301301
sb.sleep(3.2)
302302
sb.remove_elements('[data-testid="skyline-ad"]')
303303
sb.remove_elements('[data-testid="sba-container"]')

examples/cdp_mode/playwright/ReadMe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ from seleniumbase import SB
5555

5656
with SB(uc=True) as sb:
5757
sb.activate_cdp_mode()
58-
endpoint_url = sb.cdp.get_endpoint_url()
58+
endpoint_url = sb.get_endpoint_url()
5959

6060
with sync_playwright() as p:
6161
browser = p.chromium.connect_over_cdp(endpoint_url)

examples/cdp_mode/playwright/raw_basic_nested.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
with SB(uc=True) as sb:
55
sb.activate_cdp_mode()
6-
endpoint_url = sb.cdp.get_endpoint_url()
6+
endpoint_url = sb.get_endpoint_url()
77

88
with sync_playwright() as p:
99
browser = p.chromium.connect_over_cdp(endpoint_url)

examples/cdp_mode/playwright/raw_bing_cap_nested.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
with SB(uc=True, locale="en") as sb:
55
sb.activate_cdp_mode()
6-
endpoint_url = sb.cdp.get_endpoint_url()
6+
endpoint_url = sb.get_endpoint_url()
77

88
with sync_playwright() as p:
99
browser = p.chromium.connect_over_cdp(endpoint_url)

examples/cdp_mode/playwright/raw_browserscan_nested.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
with SB(uc=True, locale="en", ad_block=True) as sb:
55
sb.activate_cdp_mode()
6-
endpoint_url = sb.cdp.get_endpoint_url()
6+
endpoint_url = sb.get_endpoint_url()
77

88
with sync_playwright() as p:
99
browser = p.chromium.connect_over_cdp(endpoint_url)
1010
page = browser.contexts[0].pages[0]
11-
page.goto("https://www.browserscan.net/bot-detection")
11+
page.goto("https://browserscan.net/bot-detection")
1212
page.wait_for_timeout(500)
13-
sb.cdp.flash("Test Results", duration=3, pause=1)
13+
sb.flash("Test Results", duration=1.5, pause=0.5)
1414
sb.assert_element('strong:contains("Normal")')
1515
print("Bot Not Detected")
16-
sb.cdp.flash('strong:contains("Normal")', duration=3, pause=2)
16+
sb.flash('strong:contains("Normal")', pause=1)

examples/cdp_mode/playwright/raw_browserscan_sync.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
with sync_playwright() as p:
88
browser = p.chromium.connect_over_cdp(endpoint_url)
99
page = browser.contexts[0].pages[0]
10-
page.goto("https://www.browserscan.net/bot-detection")
10+
page.goto("https://browserscan.net/bot-detection")
1111
page.wait_for_timeout(500)
12-
sb.flash("Test Results", duration=3, pause=1)
12+
sb.flash("Test Results", duration=1.5, pause=0.5)
1313
sb.assert_element('strong:contains("Normal")')
1414
print("Bot Not Detected")
15-
sb.flash('strong:contains("Normal")', duration=3, pause=2)
15+
sb.flash('strong:contains("Normal")', pause=1)

examples/cdp_mode/playwright/raw_copilot_nested.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
with SB(uc=True) as sb:
55
sb.activate_cdp_mode()
6-
endpoint_url = sb.cdp.get_endpoint_url()
6+
endpoint_url = sb.get_endpoint_url()
77

88
with sync_playwright() as p:
99
browser = p.chromium.connect_over_cdp(endpoint_url)

examples/cdp_mode/playwright/raw_fingerprint_nested.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
with SB(uc=True, locale="en") as sb:
55
sb.activate_cdp_mode()
6-
endpoint_url = sb.cdp.get_endpoint_url()
6+
endpoint_url = sb.get_endpoint_url()
77

88
with sync_playwright() as p:
99
browser = p.chromium.connect_over_cdp(endpoint_url)
1010
page = browser.contexts[0].pages[0]
1111
page.goto("https://demo.fingerprint.com/playground")
1212
page.wait_for_timeout(500)
13-
sb.cdp.flash('a[href*="browser-bot-detection"]', duration=3, pause=1)
13+
sb.flash('a[href*="browser-bot-detection"]', duration=3, pause=1)
1414
bot_row_selector = 'table:contains("Bot") tr:nth-of-type(3)'
1515
print(sb.get_text(bot_row_selector))
1616
sb.assert_text("Bot Not detected", bot_row_selector)
17-
sb.cdp.flash(bot_row_selector, duration=3, pause=2)
17+
sb.flash(bot_row_selector, duration=3, pause=2)

examples/cdp_mode/playwright/raw_gitlab_nested.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
with SB(uc=True, locale="en") as sb:
55
sb.activate_cdp_mode()
6-
endpoint_url = sb.cdp.get_endpoint_url()
6+
endpoint_url = sb.get_endpoint_url()
77

88
with sync_playwright() as p:
99
browser = p.chromium.connect_over_cdp(endpoint_url)

0 commit comments

Comments
 (0)