Skip to content

Commit c006821

Browse files
authored
Merge pull request #4158 from seleniumbase/cdp-mode-patch-83
CDP Mode: Patch 83
2 parents 622998f + 5752ebb commit c006821

File tree

17 files changed

+201
-170
lines changed

17 files changed

+201
-170
lines changed

README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/console_scripts/ReadMe.md">🌠 Scripts</a> |
2525
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/mobile_testing.md">📱 Mobile</a>
2626
<br />
27-
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/method_summary.md">📘 APIs</a> |
28-
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/syntax_formats.md"> 🔠 Formats</a> |
27+
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/method_summary.md">📘 API</a> |
28+
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/syntax_formats.md"> 🔠 Syntaxes</a> |
2929
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/recorder_mode.md">🔴 Recorder</a> |
3030
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/example_logs/ReadMe.md">📊 Dashboard</a> |
31-
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/locale_codes.md">🗾 Locales</a> |
31+
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/locale_codes.md">🗾 Locale</a> |
3232
<a href="https://seleniumbase.io/devices/?url=seleniumbase.com">💻 Farm</a>
3333
<br />
3434
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/commander.md">🎖️ GUI</a> |
@@ -39,18 +39,17 @@
3939
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/utilities/selenium_grid/ReadMe.md">🌐 Grid</a>
4040
<br />
4141
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/how_it_works.md">👁️ How</a> |
42-
<a href="https://github.com/seleniumbase/SeleniumBase/tree/master/examples/migration/raw_selenium">🚝 Migrate</a> |
43-
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/case_plans.md">🗂️ CasePlans</a> |
44-
<a href="https://github.com/seleniumbase/SeleniumBase/tree/master/examples/boilerplates">♻️ Template</a> |
45-
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/master_qa/ReadMe.md">🧬 Hybrid</a> |
42+
<a href="https://github.com/seleniumbase/SeleniumBase/tree/master/examples/migration/raw_selenium">🚝 Migration</a> |
43+
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/playwright/ReadMe.md">🎭 Stealthy Playwright</a> |
44+
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/master_qa/ReadMe.md">🛂 MasterQA</a> |
4645
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/tour_examples/ReadMe.md">🚎 Tours</a>
4746
<br />
4847
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/integrations/github/workflows/ReadMe.md">🤖 CI/CD</a> |
4948
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/js_package_manager.md">🕹️ JSMgr</a> |
5049
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/translations.md">🌏 Translator</a> |
5150
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/presenter/ReadMe.md">🎞️ Presenter</a> |
52-
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/dialog_boxes/ReadMe.md">🛂 Dialog</a> |
53-
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/visual_testing/ReadMe.md">🖼️ Visual</a>
51+
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/visual_testing/ReadMe.md">🖼️ Visual</a> |
52+
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/case_plans.md">🗂️ CPlans</a>
5453
<br />
5554
</p>
5655

examples/cdp_mode/ReadMe.md

Lines changed: 70 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -620,63 +620,77 @@ sb.driver.stop()
620620

621621
### 🐙 <b translate="no">CDP Mode</b> Async API / Methods
622622

623+
Initialization:
624+
625+
```python
626+
from seleniumbase import cdp_driver
627+
628+
driver = await cdp_driver.start_async()
629+
tab = await driver.get(url, **kwargs)
630+
```
631+
632+
Methods: (Sometimes `tab` is named `page` in examples)
633+
623634
```python
624-
await get(url="about:blank")
625-
await open(url="about:blank")
626-
await find(text, best_match=False, timeout=10) # `text` can be a selector
627-
await find_all(text, timeout=10) # `text` can be a selector
628-
await select(selector, timeout=10)
629-
await select_all(selector, timeout=10, include_frames=False)
630-
await query_selector(selector)
631-
await query_selector_all(selector)
632-
await find_element_by_text(text, best_match=False)
633-
await find_elements_by_text(text)
634-
await reload(ignore_cache=True, script_to_evaluate_on_load=None)
635-
await evaluate(expression)
636-
await js_dumps(obj_name)
637-
await back()
638-
await forward()
639-
await get_window()
640-
await get_content()
641-
await maximize()
642-
await minimize()
643-
await fullscreen()
644-
await medimize()
645-
await set_window_size(left=0, top=0, width=1280, height=1024)
646-
await set_window_rect(left=0, top=0, width=1280, height=1024)
647-
await activate()
648-
await bring_to_front()
649-
await set_window_state(left=0, top=0, width=1280, height=720, state="normal")
650-
await get_navigation_history()
651-
await open_external_inspector() # Open a separate browser for debugging
652-
await close()
653-
await scroll_down(amount=25)
654-
await scroll_up(amount=25)
655-
await wait_for(selector="", text="", timeout=10)
656-
await download_file(url, filename=None)
657-
await save_screenshot(filename="auto", format="png", full_page=False)
658-
await print_to_pdf(filename="auto")
659-
await set_download_path(path)
660-
await get_all_linked_sources()
661-
await get_all_urls(absolute=True)
662-
await get_html()
663-
await get_page_source()
664-
await is_element_present(selector)
665-
await is_element_visible(selector)
666-
await get_element_rect(selector, timeout=5) # (relative to window)
667-
await get_window_rect()
668-
await get_gui_element_rect(selector, timeout=5) # (relative to screen)
669-
await get_title()
670-
await send_keys(selector, text, timeout=5)
671-
await type(selector, text, timeout=5)
672-
await click(selector, timeout=5)
673-
await click_with_offset(selector, x, y, center=False, timeout=5)
674-
await solve_captcha()
675-
await click_captcha() # Same as solve_captcha()
676-
await get_document()
677-
await get_flattened_document()
678-
await get_local_storage()
679-
await set_local_storage(items)
635+
await tab.get(url="about:blank")
636+
await tab.open(url="about:blank")
637+
await tab.find(text, best_match=False, timeout=10) # text can be selector
638+
await tab.find_all(text, timeout=10) # text can be selector
639+
await tab.select(selector, timeout=10)
640+
await tab.select_all(selector, timeout=10, include_frames=False)
641+
await tab.query_selector(selector)
642+
await tab.query_selector_all(selector)
643+
await tab.find_element_by_text(text, best_match=False)
644+
await tab.find_elements_by_text(text)
645+
await tab.reload(ignore_cache=True, script_to_evaluate_on_load=None)
646+
await tab.evaluate(expression)
647+
await tab.js_dumps(obj_name)
648+
await tab.back()
649+
await tab.forward()
650+
await tab.get_window()
651+
await tab.get_content()
652+
await tab.maximize()
653+
await tab.minimize()
654+
await tab.fullscreen()
655+
await tab.medimize()
656+
await tab.set_window_size(left=0, top=0, width=1280, height=1024)
657+
await tab.set_window_rect(left=0, top=0, width=1280, height=1024)
658+
await tab.activate()
659+
await tab.bring_to_front()
660+
await tab.set_window_state(
661+
left=0, top=0, width=1280, height=720, state="normal")
662+
await tab.get_navigation_history()
663+
await tab.open_external_inspector() # Open separate browser for debugging
664+
await tab.close()
665+
await tab.scroll_down(amount=25)
666+
await tab.scroll_up(amount=25)
667+
await tab.wait_for(selector="", text="", timeout=10)
668+
await tab.download_file(url, filename=None)
669+
await tab.save_screenshot(
670+
filename="auto", format="png", full_page=False)
671+
await tab.print_to_pdf(filename="auto")
672+
await tab.set_download_path(path)
673+
await tab.get_all_linked_sources()
674+
await tab.get_all_urls(absolute=True)
675+
await tab.get_html()
676+
await tab.get_page_source()
677+
await tab.is_element_present(selector)
678+
await tab.is_element_visible(selector)
679+
await tab.get_element_rect(selector, timeout=5) # (window-based)
680+
await tab.get_window_rect()
681+
await tab.get_gui_element_rect(selector, timeout=5) # (screen-based)
682+
await tab.get_title()
683+
await tab.get_current_url()
684+
await tab.send_keys(selector, text, timeout=5)
685+
await tab.type(selector, text, timeout=5)
686+
await tab.click(selector, timeout=5)
687+
await tab.click_with_offset(selector, x, y, center=False, timeout=5)
688+
await tab.solve_captcha()
689+
await tab.click_captcha() # Same as solve_captcha()
690+
await tab.get_document()
691+
await tab.get_flattened_document()
692+
await tab.get_local_storage()
693+
await tab.set_local_storage(items)
680694
```
681695

682696
--------

examples/cdp_mode/raw_async.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,27 @@
77

88
async def main():
99
url = "seleniumbase.io/simple/login"
10-
driver = await cdp_driver.start_async(incognito=True)
11-
page = await driver.get(url)
12-
print(await page.evaluate("document.title"))
13-
element = await page.select("#username")
14-
await element.send_keys_async("demo_user")
15-
element = await page.select("#password")
16-
await element.send_keys_async("secret_pass")
17-
element = await page.select("#log-in")
18-
await element.click_async()
19-
time.sleep(1)
10+
driver = await cdp_driver.start_async()
11+
page = await driver.get(url, lang="en")
12+
print(await page.get_title())
13+
await page.type("#username", "demo_user")
14+
await page.type("#password", "secret_pass")
15+
await page.click("#log-in")
16+
print(await page.get_title())
2017
element = await page.select("h1")
2118
assert element.text == "Welcome!"
19+
top_nav = await page.select("div.topnav")
20+
links = await top_nav.query_selector_all_async("a")
21+
for nav_item in links:
22+
print(nav_item.text)
2223
driver.stop()
2324

2425
if __name__ == "__main__":
2526
# Call an async function with awaited methods
2627
loop = asyncio.new_event_loop()
2728
loop.run_until_complete(main())
2829

29-
# Call everything without using async / await
30+
# An example of wrapping all async calls with event loops
3031
driver = cdp_driver.start_sync()
3132
page = loop.run_until_complete(driver.get("about:blank"))
3233
loop.run_until_complete(page.set_locale("en"))

examples/cdp_mode/raw_basic_async.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ async def main():
1818
links = await top_nav.query_selector_all_async("a")
1919
for nav_item in links:
2020
print(nav_item.text)
21+
driver.stop()
2122

2223
if __name__ == "__main__":
2324
# Call an async function with awaited methods

examples/cdp_mode/raw_cookies_async.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@ async def get_login_cookies():
99
url = "https://www.saucedemo.com"
1010
driver = await cdp_driver.start_async(incognito=True)
1111
page = await driver.get(url)
12-
element = await page.select("#user-name")
13-
await element.send_keys_async("standard_user")
14-
element = await page.select("#password")
15-
await element.send_keys_async("secret_sauce")
16-
element = await page.select('input[type="submit"]')
17-
await element.click_async()
12+
await page.type("#user-name", "standard_user")
13+
await page.type("#password", "secret_sauce")
14+
await page.click('input[type="submit"]')
1815
cookies = await driver.cookies.get_all()
1916
driver.stop()
2017
return cookies

examples/cdp_mode/raw_mobile_async.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,14 @@ async def main():
1515
)
1616
page = await driver.get(url, lang="en")
1717
time.sleep(3)
18-
try:
19-
element = await page.select('[style*="grid"] div div', timeout=1)
20-
await element.mouse_click_with_offset_async(32, 28)
21-
except Exception:
22-
pass # Maybe CAPTCHA was already bypassed
18+
await page.solve_captcha()
2319
element = await page.select('label[for="user_login"]')
2420
await element.flash_async(duration=1.5, color="44EE44")
2521
time.sleep(1)
2622
element = await page.select('[data-testid="sign-in-button"]')
2723
await element.flash_async(duration=2, color="44EE44")
2824
time.sleep(2)
25+
driver.stop()
2926

3027
if __name__ == "__main__":
3128
# Call an async function with awaited methods

examples/cdp_mode/raw_multi_async.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ async def main(url):
1111
page = await driver.get(url)
1212
await page.set_window_rect(randint(4, 600), randint(8, 410), 860, 500)
1313
await page.sleep(0.5)
14-
field = await page.select("input")
15-
await field.send_keys_async("Text")
16-
button = await page.select("button")
17-
await button.click_async()
14+
await page.type("input", "Text")
15+
await page.click("button")
1816
await page.sleep(2)
1917
driver.stop()
2018

help_docs/ReadMe.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
<a href="https://seleniumbase.io/help_docs/recorder_mode/">🔴 Recorder</a> |
3636
<a href="https://seleniumbase.io/devices/?url=seleniumbase.io">💻 Device Farm</a>
3737
<br />
38+
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/playwright/ReadMe.md">🎭 Stealthy Playwright Mode</a>
39+
<br />
3840
<a href="https://seleniumbase.io/examples/presenter/ReadMe/">🎞️ Slides</a> |
3941
<a href="https://seleniumbase.io/examples/chart_maker/ReadMe/">📶 Chart Maker</a>
4042
<br / >

0 commit comments

Comments
 (0)