Skip to content

Commit 06fb08f

Browse files
authored
Merge pull request #4303 from seleniumbase/cdp-mode-patch-102
CDP Mode: Patch 102
2 parents d8bf665 + ce0074a commit 06fb08f

32 files changed

+195
-102
lines changed

examples/cdp_mode/ReadMe.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ sb.cdp.get_current_url()
471471
sb.cdp.get_origin()
472472
sb.cdp.get_html(include_shadow_dom=True)
473473
sb.cdp.get_page_source(include_shadow_dom=True)
474+
sb.cdp.get_beautiful_soup(source=None)
474475
sb.cdp.get_user_agent()
475476
sb.cdp.get_cookie_string()
476477
sb.cdp.get_locale_code()

examples/cdp_mode/playwright/ReadMe.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ The `SB()` format requires WebDriver, therefore `chromedriver` will be downloade
8888

8989
In the sync formats, `get_endpoint_url()` also applies `nest-asyncio` so that nested event loops are allowed. (Python doesn't allow nested event loops by default). Without this, you'd get the error: `"Cannot run the event loop while another loop is running"` when calling CDP Mode methods (such as `solve_captcha()`) from within the Playwright context manager. This `nest-asyncio` call is done behind-the-scenes so that users don't need to handle this on their own.
9090

91+
Default timeout values are different between Playwright and SeleniumBase. For instance, a 30-second default timeout in a Playwright method might be 10 seconds in the equivalent SeleniumBase method. When specifying custom timeout values, Playwright uses milliseconds, whereas SeleniumBase uses seconds. Eg. `page.wait_for_timeout(500)` is the equivalent of `sb.sleep(0.5)`.
92+
93+
Playwright's `:has-text()` selector is the equivalent of SeleniumBase's `:contains()` selector, except for one small difference: `:has-text()` isn't case-sensitive, but `:contains()` is.
94+
95+
Unlike normal Playwright, you don't need to run `playwright install` before running Stealthy Playwright Mode scripts because the system Chrome will be used. There's also the option of setting `use_chromium=True` to use the unbranded Chromium browser instead, which still supports extensions.
96+
9197
### 🎭 <b translate="no">Stealthy Playwright Mode</b> examples:
9298

9399
Here's an example that queries Microsoft Copilot:
@@ -105,16 +111,16 @@ with sync_playwright() as p:
105111
page = context.pages[0]
106112
page.goto("https://copilot.microsoft.com")
107113
page.wait_for_selector("textarea#userInput")
108-
sb.sleep(1)
114+
page.wait_for_timeout(1000)
109115
query = "Playwright Python connect_over_cdp() sync example"
110116
page.fill("textarea#userInput", query)
111117
page.click('button[data-testid="submit-button"]')
112-
sb.sleep(3)
118+
page.wait_for_timeout(4000)
113119
sb.solve_captcha()
114120
page.wait_for_selector('button[data-testid*="-thumbs-up"]')
115-
sb.sleep(4)
121+
page.wait_for_timeout(4000)
116122
page.click('button[data-testid*="scroll-to-bottom"]')
117-
sb.sleep(3)
123+
page.wait_for_timeout(3000)
118124
chat_results = '[data-testid="highlighted-chats"]'
119125
result = page.locator(chat_results).inner_text()
120126
print(result.replace("\n\n", " \n"))
@@ -134,9 +140,9 @@ with sync_playwright() as p:
134140
context = browser.contexts[0]
135141
page = context.pages[0]
136142
page.goto("https://www.bing.com/turing/captcha/challenge")
137-
sb.sleep(3)
143+
page.wait_for_timeout(2000)
138144
sb.solve_captcha()
139-
sb.sleep(3)
145+
page.wait_for_timeout(2000)
140146
```
141147

142148
--------

examples/cdp_mode/playwright/raw_basic_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ async def main():
1616
await page.fill("#password", "secret_pass")
1717
await page.click("#log-in")
1818
await page.wait_for_selector("h1")
19-
await driver.sleep(1)
19+
await page.wait_for_timeout(1000)
2020

2121

2222
if __name__ == "__main__":

examples/cdp_mode/playwright/raw_basic_nested.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
page.fill("#password", "secret_pass")
1515
page.click("#log-in")
1616
page.wait_for_selector("h1")
17-
sb.sleep(1)
17+
page.wait_for_timeout(1000)

examples/cdp_mode/playwright/raw_basic_sync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
page.fill("#password", "secret_pass")
1414
page.click("#log-in")
1515
page.wait_for_selector("h1")
16-
sb.sleep(1)
16+
page.wait_for_timeout(1000)

examples/cdp_mode/playwright/raw_bing_cap_async.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ async def main():
1212
context = browser.contexts[0]
1313
page = context.pages[0]
1414
await page.goto("https://www.bing.com/turing/captcha/challenge")
15-
await driver.sleep(3)
15+
await page.wait_for_timeout(2000)
1616
await driver.solve_captcha()
17-
await driver.sleep(3)
17+
await page.wait_for_timeout(2000)
1818

1919

2020
if __name__ == "__main__":

examples/cdp_mode/playwright/raw_bing_cap_nested.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
context = browser.contexts[0]
1111
page = context.pages[0]
1212
page.goto("https://www.bing.com/turing/captcha/challenge")
13-
sb.sleep(3)
13+
page.wait_for_timeout(2000)
1414
sb.solve_captcha()
15-
sb.sleep(3)
15+
page.wait_for_timeout(2000)

examples/cdp_mode/playwright/raw_bing_cap_sync.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
context = browser.contexts[0]
1010
page = context.pages[0]
1111
page.goto("https://www.bing.com/turing/captcha/challenge")
12-
sb.sleep(3)
12+
page.wait_for_timeout(2000)
1313
sb.solve_captcha()
14-
sb.sleep(3)
14+
page.wait_for_timeout(2000)

examples/cdp_mode/playwright/raw_cf_cap_sync.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
context = browser.contexts[0]
1010
page = context.pages[0]
1111
page.goto("https://www.cloudflare.com/login")
12-
sb.sleep(3)
12+
page.wait_for_timeout(4500)
1313
sb.solve_captcha()
14-
sb.sleep(3)
14+
page.wait_for_timeout(3000)

examples/cdp_mode/playwright/raw_copilot_async.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ async def main():
1313
page = context.pages[0]
1414
await page.goto("https://copilot.microsoft.com")
1515
await page.wait_for_selector("textarea#userInput")
16-
await driver.sleep(1)
16+
await page.wait_for_timeout(1000)
1717
query = "Playwright Python connect_over_cdp() sync example"
1818
await page.fill("textarea#userInput", query)
1919
await page.click('button[data-testid="submit-button"]')
20-
await driver.sleep(4)
20+
await page.wait_for_timeout(4000)
2121
await driver.solve_captcha()
2222
await page.wait_for_selector('button[data-testid*="-thumbs-up"]')
23-
await driver.sleep(4)
23+
await page.wait_for_timeout(4000)
2424
await page.click('button[data-testid*="scroll-to-bottom"]')
25-
await driver.sleep(3)
25+
await page.wait_for_timeout(3000)
2626
chat_results = '[data-testid="highlighted-chats"]'
2727
result = await page.locator(chat_results).inner_text()
2828
print(result.replace("\n\n", " \n"))

0 commit comments

Comments
 (0)