Skip to content

Commit 7fdb25f

Browse files
committed
Update CDP examples
1 parent a059b6f commit 7fdb25f

File tree

5 files changed

+58
-7
lines changed

5 files changed

+58
-7
lines changed

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)