Skip to content

Commit

Permalink
Merge pull request #3367 from seleniumbase/cdp-mode-patch-22
Browse files Browse the repository at this point in the history
CDP Mode - Patch 22
  • Loading branch information
mdmintz authored Dec 25, 2024
2 parents 05bb565 + 1a845dd commit 35dcd82
Show file tree
Hide file tree
Showing 15 changed files with 260 additions and 506 deletions.
60 changes: 29 additions & 31 deletions examples/cdp_mode/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,25 +345,23 @@ with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:

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

(Some method args have been left out for simplicity. Eg: <code translate="no">timeout</code>)

```python
sb.cdp.get(url)
sb.cdp.open(url)
sb.cdp.reload(ignore_cache=True, script_to_evaluate_on_load=None)
sb.cdp.refresh()
sb.cdp.get_event_loop()
sb.cdp.add_handler(event, handler)
sb.cdp.find_element(selector)
sb.cdp.find(selector)
sb.cdp.locator(selector)
sb.cdp.find_element_by_text(text, tag_name=None)
sb.cdp.find_all(selector)
sb.cdp.find_element(selector, best_match=False, timeout=None)
sb.cdp.find(selector, best_match=False, timeout=None)
sb.cdp.locator(selector, best_match=False, timeout=None)
sb.cdp.find_element_by_text(text, tag_name=None, timeout=None)
sb.cdp.find_all(selector, timeout=None)
sb.cdp.find_elements_by_text(text, tag_name=None)
sb.cdp.select(selector)
sb.cdp.select_all(selector)
sb.cdp.find_elements(selector)
sb.cdp.find_visible_elements(selector)
sb.cdp.select(selector, timeout=None)
sb.cdp.select_all(selector, timeout=None)
sb.cdp.find_elements(selector, timeout=None)
sb.cdp.find_visible_elements(selector, timeout=None)
sb.cdp.click_nth_element(selector, number)
sb.cdp.click_nth_visible_element(selector, number)
sb.cdp.click_link(link_text)
Expand All @@ -375,17 +373,17 @@ sb.cdp.get_all_cookies(*args, **kwargs)
sb.cdp.set_all_cookies(*args, **kwargs)
sb.cdp.save_cookies(*args, **kwargs)
sb.cdp.load_cookies(*args, **kwargs)
sb.cdp.clear_cookies(*args, **kwargs)
sb.cdp.clear_cookies()
sb.cdp.sleep(seconds)
sb.cdp.bring_active_window_to_front()
sb.cdp.bring_to_front()
sb.cdp.get_active_element()
sb.cdp.get_active_element_css()
sb.cdp.click(selector)
sb.cdp.click(selector, timeout=None)
sb.cdp.click_active_element()
sb.cdp.click_if_visible(selector)
sb.cdp.click_visible_elements(selector, limit=0)
sb.cdp.mouse_click(selector)
sb.cdp.mouse_click(selector, timeout=None)
sb.cdp.nested_click(parent_selector, selector)
sb.cdp.get_nested_element(parent_selector, selector)
sb.cdp.select_option_by_text(dropdown_selector, option)
Expand All @@ -396,10 +394,10 @@ sb.cdp.highlight_overlay(selector)
sb.cdp.remove_element(selector)
sb.cdp.remove_from_dom(selector)
sb.cdp.remove_elements(selector)
sb.cdp.send_keys(selector, text)
sb.cdp.press_keys(selector, text)
sb.cdp.type(selector, text)
sb.cdp.set_value(selector, text)
sb.cdp.send_keys(selector, text, timeout=None)
sb.cdp.press_keys(selector, text, timeout=None)
sb.cdp.type(selector, text, timeout=None)
sb.cdp.set_value(selector, text, timeout=None)
sb.cdp.evaluate(expression)
sb.cdp.js_dumps(obj_name)
sb.cdp.maximize()
Expand All @@ -420,11 +418,11 @@ sb.cdp.get_screen_rect()
sb.cdp.get_window_rect()
sb.cdp.get_window_size()
sb.cdp.get_window_position()
sb.cdp.get_element_rect(selector)
sb.cdp.get_element_size(selector)
sb.cdp.get_element_position(selector)
sb.cdp.get_gui_element_rect(selector)
sb.cdp.get_gui_element_center(selector)
sb.cdp.get_element_rect(selector, timeout=None)
sb.cdp.get_element_size(selector, timeout=None)
sb.cdp.get_element_position(selector, timeout=None)
sb.cdp.get_gui_element_rect(selector, timeout=None)
sb.cdp.get_gui_element_center(selector, timeout=None)
sb.cdp.get_document()
sb.cdp.get_flattened_document()
sb.cdp.get_element_attributes(selector)
Expand Down Expand Up @@ -452,19 +450,19 @@ sb.cdp.uncheck_if_checked(selector)
sb.cdp.unselect_if_selected(selector)
sb.cdp.is_element_present(selector)
sb.cdp.is_element_visible(selector)
sb.cdp.wait_for_element_visible(selector)
sb.cdp.assert_element(selector)
sb.cdp.assert_element_visible(selector)
sb.cdp.assert_element_present(selector)
sb.cdp.assert_element_absent(selector)
sb.cdp.assert_element_not_visible(selector)
sb.cdp.wait_for_element_visible(selector, timeout=None)
sb.cdp.assert_element(selector, timeout=None)
sb.cdp.assert_element_visible(selector, timeout=None)
sb.cdp.assert_element_present(selector, timeout=None)
sb.cdp.assert_element_absent(selector, timeout=None)
sb.cdp.assert_element_not_visible(selector, timeout=None)
sb.cdp.assert_element_attribute(selector, attribute, value=None)
sb.cdp.assert_title(title)
sb.cdp.assert_title_contains(substring)
sb.cdp.assert_url(url)
sb.cdp.assert_url_contains(substring)
sb.cdp.assert_text(text, selector="html")
sb.cdp.assert_exact_text(text, selector="html")
sb.cdp.assert_text(text, selector="html", timeout=None)
sb.cdp.assert_exact_text(text, selector="html", timeout=None)
sb.cdp.assert_true()
sb.cdp.assert_false()
sb.cdp.assert_equal(first, second)
Expand Down
11 changes: 11 additions & 0 deletions examples/cdp_mode/raw_gettyimages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en", pls="none") as sb:
sb.activate_cdp_mode("https://www.gettyimages.com/")
sb.cdp.click('label:contains("Editorial")')
sb.cdp.press_keys("form input", "comic con 2024 sci fi panel\n")
sb.sleep(3)
items = sb.cdp.find_elements("figure picture img")
for item in items:
item.flash(color="44CC88")
sb.sleep(0.08)
25 changes: 25 additions & 0 deletions examples/cdp_mode/raw_kohls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
url = "https://www.kohls.com/"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
search = "Mickey Mouse 100 friends teal pillow"
required_text = "Mickey"
sb.cdp.press_keys('input[name="search"]', search + "\n")
sb.sleep(5)
for item in sb.cdp.find_elements("div.products-container-right"):
if "Sponsored" in item.text:
item.remove_from_dom()
sb.cdp.remove_elements("#tce-sticky-wrapper")
sb.cdp.remove_elements("li.sponsored-product")
sb.cdp.remove_elements("#tce-dec-ces-3-banner")
print('*** Kohls Search for "%s":' % search)
for item in sb.cdp.find_elements("ul.products a img"):
if item:
item.flash(color="44CC88")
title = item.get_attribute("title")
if title and required_text in title:
print("* " + title)
sb.sleep(0.1)
sb.sleep(1)
11 changes: 5 additions & 6 deletions examples/cdp_mode/raw_req_mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@

async def request_paused_handler(event, tab):
r = event.request
rid = event.request_id
is_image = ".png" in r.url or ".jpg" in r.url or ".gif" in r.url
if not is_image: # Let the data through
tab.feed_cdp(mycdp.fetch.continue_request(request_id=event.request_id))
else: # Modify the data (change the image)
tab.feed_cdp(mycdp.fetch.continue_request(
request_id=event.request_id,
url="https://seleniumbase.io/other/with_frakes.jpg"
))
tab.feed_cdp(mycdp.fetch.continue_request(request_id=rid))
else: # Modify the data (Change the image URL)
new_url = "https://seleniumbase.io/other/with_frakes.jpg"
tab.feed_cdp(mycdp.fetch.continue_request(request_id=rid, url=new_url))


with SB(uc=True, test=True, locale_code="en", pls="none") as sb:
Expand Down
16 changes: 16 additions & 0 deletions examples/cdp_mode/raw_theaters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Simple web-scraping example in CDP Mode"""
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
url = "https://architectureofcities.com/roman-theaters"
sb.activate_cdp_mode(url)
sb.cdp.click_if_visible("#cn-close-notice")
sb.sleep(2)
print("*** " + sb.cdp.get_text("h1") + " ***")
for item in sb.cdp.find_elements("h3"):
if item.text and "." in item.text:
item.flash(color="44CC88")
sb.cdp.scroll_down(34)
print("* " + item.text.replace(" ", " "))
sb.sleep(0.15)
sb.sleep(1)
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_tiktok.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
sb.cdp.click_if_visible('button:contains("Refresh")')
sb.sleep(1.5)
print(sb.cdp.get_text('h2[data-e2e="user-bio"]'))
for i in range(50):
for i in range(54):
sb.cdp.scroll_down(12)
sb.sleep(1)
Loading

0 comments on commit 35dcd82

Please sign in to comment.