Skip to content

Commit 35dcd82

Browse files
authored
Merge pull request #3367 from seleniumbase/cdp-mode-patch-22
CDP Mode - Patch 22
2 parents 05bb565 + 1a845dd commit 35dcd82

15 files changed

+260
-506
lines changed

examples/cdp_mode/ReadMe.md

+29-31
Original file line numberDiff line numberDiff line change
@@ -345,25 +345,23 @@ with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
345345

346346
### 🐙 <b translate="no">CDP Mode</b> API / Methods
347347

348-
(Some method args have been left out for simplicity. Eg: <code translate="no">timeout</code>)
349-
350348
```python
351349
sb.cdp.get(url)
352350
sb.cdp.open(url)
353351
sb.cdp.reload(ignore_cache=True, script_to_evaluate_on_load=None)
354352
sb.cdp.refresh()
355353
sb.cdp.get_event_loop()
356354
sb.cdp.add_handler(event, handler)
357-
sb.cdp.find_element(selector)
358-
sb.cdp.find(selector)
359-
sb.cdp.locator(selector)
360-
sb.cdp.find_element_by_text(text, tag_name=None)
361-
sb.cdp.find_all(selector)
355+
sb.cdp.find_element(selector, best_match=False, timeout=None)
356+
sb.cdp.find(selector, best_match=False, timeout=None)
357+
sb.cdp.locator(selector, best_match=False, timeout=None)
358+
sb.cdp.find_element_by_text(text, tag_name=None, timeout=None)
359+
sb.cdp.find_all(selector, timeout=None)
362360
sb.cdp.find_elements_by_text(text, tag_name=None)
363-
sb.cdp.select(selector)
364-
sb.cdp.select_all(selector)
365-
sb.cdp.find_elements(selector)
366-
sb.cdp.find_visible_elements(selector)
361+
sb.cdp.select(selector, timeout=None)
362+
sb.cdp.select_all(selector, timeout=None)
363+
sb.cdp.find_elements(selector, timeout=None)
364+
sb.cdp.find_visible_elements(selector, timeout=None)
367365
sb.cdp.click_nth_element(selector, number)
368366
sb.cdp.click_nth_visible_element(selector, number)
369367
sb.cdp.click_link(link_text)
@@ -375,17 +373,17 @@ sb.cdp.get_all_cookies(*args, **kwargs)
375373
sb.cdp.set_all_cookies(*args, **kwargs)
376374
sb.cdp.save_cookies(*args, **kwargs)
377375
sb.cdp.load_cookies(*args, **kwargs)
378-
sb.cdp.clear_cookies(*args, **kwargs)
376+
sb.cdp.clear_cookies()
379377
sb.cdp.sleep(seconds)
380378
sb.cdp.bring_active_window_to_front()
381379
sb.cdp.bring_to_front()
382380
sb.cdp.get_active_element()
383381
sb.cdp.get_active_element_css()
384-
sb.cdp.click(selector)
382+
sb.cdp.click(selector, timeout=None)
385383
sb.cdp.click_active_element()
386384
sb.cdp.click_if_visible(selector)
387385
sb.cdp.click_visible_elements(selector, limit=0)
388-
sb.cdp.mouse_click(selector)
386+
sb.cdp.mouse_click(selector, timeout=None)
389387
sb.cdp.nested_click(parent_selector, selector)
390388
sb.cdp.get_nested_element(parent_selector, selector)
391389
sb.cdp.select_option_by_text(dropdown_selector, option)
@@ -396,10 +394,10 @@ sb.cdp.highlight_overlay(selector)
396394
sb.cdp.remove_element(selector)
397395
sb.cdp.remove_from_dom(selector)
398396
sb.cdp.remove_elements(selector)
399-
sb.cdp.send_keys(selector, text)
400-
sb.cdp.press_keys(selector, text)
401-
sb.cdp.type(selector, text)
402-
sb.cdp.set_value(selector, text)
397+
sb.cdp.send_keys(selector, text, timeout=None)
398+
sb.cdp.press_keys(selector, text, timeout=None)
399+
sb.cdp.type(selector, text, timeout=None)
400+
sb.cdp.set_value(selector, text, timeout=None)
403401
sb.cdp.evaluate(expression)
404402
sb.cdp.js_dumps(obj_name)
405403
sb.cdp.maximize()
@@ -420,11 +418,11 @@ sb.cdp.get_screen_rect()
420418
sb.cdp.get_window_rect()
421419
sb.cdp.get_window_size()
422420
sb.cdp.get_window_position()
423-
sb.cdp.get_element_rect(selector)
424-
sb.cdp.get_element_size(selector)
425-
sb.cdp.get_element_position(selector)
426-
sb.cdp.get_gui_element_rect(selector)
427-
sb.cdp.get_gui_element_center(selector)
421+
sb.cdp.get_element_rect(selector, timeout=None)
422+
sb.cdp.get_element_size(selector, timeout=None)
423+
sb.cdp.get_element_position(selector, timeout=None)
424+
sb.cdp.get_gui_element_rect(selector, timeout=None)
425+
sb.cdp.get_gui_element_center(selector, timeout=None)
428426
sb.cdp.get_document()
429427
sb.cdp.get_flattened_document()
430428
sb.cdp.get_element_attributes(selector)
@@ -452,19 +450,19 @@ sb.cdp.uncheck_if_checked(selector)
452450
sb.cdp.unselect_if_selected(selector)
453451
sb.cdp.is_element_present(selector)
454452
sb.cdp.is_element_visible(selector)
455-
sb.cdp.wait_for_element_visible(selector)
456-
sb.cdp.assert_element(selector)
457-
sb.cdp.assert_element_visible(selector)
458-
sb.cdp.assert_element_present(selector)
459-
sb.cdp.assert_element_absent(selector)
460-
sb.cdp.assert_element_not_visible(selector)
453+
sb.cdp.wait_for_element_visible(selector, timeout=None)
454+
sb.cdp.assert_element(selector, timeout=None)
455+
sb.cdp.assert_element_visible(selector, timeout=None)
456+
sb.cdp.assert_element_present(selector, timeout=None)
457+
sb.cdp.assert_element_absent(selector, timeout=None)
458+
sb.cdp.assert_element_not_visible(selector, timeout=None)
461459
sb.cdp.assert_element_attribute(selector, attribute, value=None)
462460
sb.cdp.assert_title(title)
463461
sb.cdp.assert_title_contains(substring)
464462
sb.cdp.assert_url(url)
465463
sb.cdp.assert_url_contains(substring)
466-
sb.cdp.assert_text(text, selector="html")
467-
sb.cdp.assert_exact_text(text, selector="html")
464+
sb.cdp.assert_text(text, selector="html", timeout=None)
465+
sb.cdp.assert_exact_text(text, selector="html", timeout=None)
468466
sb.cdp.assert_true()
469467
sb.cdp.assert_false()
470468
sb.cdp.assert_equal(first, second)

examples/cdp_mode/raw_gettyimages.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from seleniumbase import SB
2+
3+
with SB(uc=True, test=True, locale_code="en", pls="none") as sb:
4+
sb.activate_cdp_mode("https://www.gettyimages.com/")
5+
sb.cdp.click('label:contains("Editorial")')
6+
sb.cdp.press_keys("form input", "comic con 2024 sci fi panel\n")
7+
sb.sleep(3)
8+
items = sb.cdp.find_elements("figure picture img")
9+
for item in items:
10+
item.flash(color="44CC88")
11+
sb.sleep(0.08)

examples/cdp_mode/raw_kohls.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from seleniumbase import SB
2+
3+
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
4+
url = "https://www.kohls.com/"
5+
sb.activate_cdp_mode(url)
6+
sb.sleep(2.5)
7+
search = "Mickey Mouse 100 friends teal pillow"
8+
required_text = "Mickey"
9+
sb.cdp.press_keys('input[name="search"]', search + "\n")
10+
sb.sleep(5)
11+
for item in sb.cdp.find_elements("div.products-container-right"):
12+
if "Sponsored" in item.text:
13+
item.remove_from_dom()
14+
sb.cdp.remove_elements("#tce-sticky-wrapper")
15+
sb.cdp.remove_elements("li.sponsored-product")
16+
sb.cdp.remove_elements("#tce-dec-ces-3-banner")
17+
print('*** Kohls Search for "%s":' % search)
18+
for item in sb.cdp.find_elements("ul.products a img"):
19+
if item:
20+
item.flash(color="44CC88")
21+
title = item.get_attribute("title")
22+
if title and required_text in title:
23+
print("* " + title)
24+
sb.sleep(0.1)
25+
sb.sleep(1)

examples/cdp_mode/raw_req_mod.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55

66
async def request_paused_handler(event, tab):
77
r = event.request
8+
rid = event.request_id
89
is_image = ".png" in r.url or ".jpg" in r.url or ".gif" in r.url
910
if not is_image: # Let the data through
10-
tab.feed_cdp(mycdp.fetch.continue_request(request_id=event.request_id))
11-
else: # Modify the data (change the image)
12-
tab.feed_cdp(mycdp.fetch.continue_request(
13-
request_id=event.request_id,
14-
url="https://seleniumbase.io/other/with_frakes.jpg"
15-
))
11+
tab.feed_cdp(mycdp.fetch.continue_request(request_id=rid))
12+
else: # Modify the data (Change the image URL)
13+
new_url = "https://seleniumbase.io/other/with_frakes.jpg"
14+
tab.feed_cdp(mycdp.fetch.continue_request(request_id=rid, url=new_url))
1615

1716

1817
with SB(uc=True, test=True, locale_code="en", pls="none") as sb:

examples/cdp_mode/raw_theaters.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""Simple web-scraping example in CDP Mode"""
2+
from seleniumbase import SB
3+
4+
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
5+
url = "https://architectureofcities.com/roman-theaters"
6+
sb.activate_cdp_mode(url)
7+
sb.cdp.click_if_visible("#cn-close-notice")
8+
sb.sleep(2)
9+
print("*** " + sb.cdp.get_text("h1") + " ***")
10+
for item in sb.cdp.find_elements("h3"):
11+
if item.text and "." in item.text:
12+
item.flash(color="44CC88")
13+
sb.cdp.scroll_down(34)
14+
print("* " + item.text.replace(" ", " "))
15+
sb.sleep(0.15)
16+
sb.sleep(1)

examples/cdp_mode/raw_tiktok.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
sb.cdp.click_if_visible('button:contains("Refresh")')
88
sb.sleep(1.5)
99
print(sb.cdp.get_text('h2[data-e2e="user-bio"]'))
10-
for i in range(50):
10+
for i in range(54):
1111
sb.cdp.scroll_down(12)
1212
sb.sleep(1)

0 commit comments

Comments
 (0)