Skip to content

Commit 1116fad

Browse files
authored
Merge pull request #4020 from seleniumbase/cdp-mode-patch-64
CDP Mode: Patch 64
2 parents ebb4e6b + fd86a08 commit 1116fad

27 files changed

+367
-49
lines changed

examples/cdp_mode/ReadMe.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ sb.cdp.clear_input(selector, timeout=None)
421421
sb.cdp.clear(selector, timeout=None)
422422
sb.cdp.submit(selector)
423423
sb.cdp.evaluate(expression)
424+
sb.cdp.execute_script(expression)
424425
sb.cdp.js_dumps(obj_name)
425426
sb.cdp.maximize()
426427
sb.cdp.minimize()
@@ -464,6 +465,9 @@ sb.cdp.get_attribute(selector, attribute)
464465
sb.cdp.get_element_html(selector)
465466
sb.cdp.get_mfa_code(totp_key=None)
466467
sb.cdp.enter_mfa_code(selector, totp_key=None, timeout=None)
468+
sb.cdp.activate_messenger()
469+
sb.cdp.set_messenger_theme(theme="default", location="default")
470+
sb.cdp.post_message(message, duration=None, pause=True, style="info")
467471
sb.cdp.set_locale(locale)
468472
sb.cdp.set_local_storage_item(key, value)
469473
sb.cdp.set_session_storage_item(key, value)
@@ -528,6 +532,8 @@ sb.cdp.scroll_to_top()
528532
sb.cdp.scroll_to_bottom()
529533
sb.cdp.scroll_up(amount=25)
530534
sb.cdp.scroll_down(amount=25)
535+
sb.cdp.save_page_source(name, folder=None)
536+
sb.cdp.save_as_html(name, folder=None)
531537
sb.cdp.save_screenshot(name, folder=None, selector=None)
532538
sb.cdp.print_to_pdf(name, folder=None)
533539
sb.cdp.save_as_pdf(name, folder=None)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import mycdp
2+
from seleniumbase import decorators
3+
from seleniumbase import sb_cdp
4+
5+
6+
async def block_urls(tab):
7+
await tab.send(mycdp.network.enable())
8+
await tab.send(mycdp.network.set_blocked_urls(
9+
urls=[
10+
"*googlesyndication.com*",
11+
"*googletagmanager.com*",
12+
"*google-analytics.com*",
13+
"*amazon-adsystem.com*",
14+
"*adsafeprotected.com*",
15+
"*doubleclick.net*",
16+
"*fastclick.net*",
17+
"*snigelweb.com*",
18+
"*2mdn.net*",
19+
]
20+
))
21+
22+
with decorators.print_runtime("raw_ad_blocking.py"):
23+
sb = sb_cdp.Chrome()
24+
loop = sb.get_event_loop()
25+
loop.run_until_complete(block_urls(sb.get_active_tab()))
26+
sb.open("https://www.w3schools.com/jquery/default.asp")
27+
source = sb.get_page_source()
28+
sb.assert_true("doubleclick.net" not in source)
29+
sb.assert_true("google-analytics.com" not in source)
30+
sb.post_message("Blocking was successful!")
31+
sb.driver.quit()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from seleniumbase import sb_cdp
2+
3+
url = "https://copilot.microsoft.com/"
4+
sb = sb_cdp.Chrome(url, locale="en", guest=True)
5+
textarea = "textarea#userInput"
6+
sb.wait_for_element(textarea)
7+
sb.sleep(1.5)
8+
sb.click_if_visible('[aria-label="Dismiss"]')
9+
sb.sleep(0.5)
10+
sb.click('button[data-testid*="chat-mode-"]')
11+
sb.sleep(1.1)
12+
sb.click('button[title="Think Deeper"]')
13+
sb.sleep(1.1)
14+
query = "How to start automating with SeleniumBase?"
15+
sb.press_keys(textarea, query)
16+
sb.sleep(1.1)
17+
sb.click('button[data-testid="submit-button"]')
18+
sb.sleep(2.5)
19+
sb.gui_click_captcha()
20+
sb.sleep(2.5)
21+
sb.gui_click_captcha()
22+
sb.sleep(3.5)
23+
stop_button = '[data-testid="stop-button"]'
24+
thumbs_up = 'button[data-testid*="-thumbs-up-"]'
25+
sb.wait_for_element_absent(stop_button, timeout=45)
26+
sb.wait_for_element(thumbs_up, timeout=20)
27+
sb.sleep(0.6)
28+
sb.click('button[data-testid*="scroll-to-bottom"]')
29+
sb.sleep(2.2)
30+
folder = "downloaded_files"
31+
file_name = "copilot_results.html"
32+
sb.save_as_html(file_name, folder)
33+
print('"./%s/%s" was saved!' % (folder, file_name))
34+
sb.driver.stop()

examples/cdp_mode/raw_cdp_hyatt.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from seleniumbase import sb_cdp
2+
3+
url = "https://www.hyatt.com/"
4+
sb = sb_cdp.Chrome(url, locale="en", guest=True)
5+
sb.sleep(4.2)
6+
sb.click_if_visible('button[aria-label="Close"]')
7+
sb.click_if_visible("#onetrust-reject-all-handler")
8+
sb.sleep(1)
9+
location = "Anaheim, CA, USA"
10+
sb.type('input[id="search-term"]', location)
11+
sb.sleep(1)
12+
sb.click('li[data-js="suggestion"]')
13+
sb.sleep(1)
14+
sb.click("button.be-button-shop")
15+
sb.sleep(6)
16+
card_info = 'div[data-booking-status="BOOKABLE"] [class*="HotelCard_info"]'
17+
hotels = sb.select_all(card_info)
18+
print("Hyatt Hotels in %s:" % location)
19+
print("(" + sb.get_text("ul.b-color_text-white") + ")")
20+
if len(hotels) == 0:
21+
print("No availability over the selected dates!")
22+
for hotel in hotels:
23+
info = hotel.text.strip()
24+
if "Avg/Night" in info and not info.startswith("Rates from"):
25+
name = info.split(" (")[0].split(" + ")[0].split(" Award Cat")[0]
26+
name = name.split(" Rates from :")[0]
27+
price = "?"
28+
if "Rates from : " in info:
29+
price = info.split("Rates from : ")[1].split(" Avg/Night")[0]
30+
print("* %s => %s" % (name, price))
31+
sb.driver.stop()

examples/cdp_mode/raw_cdp_kohls.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from seleniumbase import sb_cdp
2+
3+
url = "https://www.kohls.com/"
4+
sb = sb_cdp.Chrome(url, locale="en", guest=True)
5+
sb.sleep(2.8)
6+
search = "Mickey Mouse 100 friends teal pillow"
7+
required_text = "Mickey"
8+
sb.press_keys('input[name="search"]', search + "\n")
9+
sb.sleep(5)
10+
for item in sb.find_elements("div.products-container-right"):
11+
if "Sponsored" in item.text:
12+
item.remove_from_dom()
13+
sb.remove_elements("#tce-sticky-wrapper")
14+
sb.remove_elements("li.sponsored-product")
15+
sb.remove_elements("#tce-dec-ces-3-banner")
16+
print('*** Kohls Search for "%s":' % search)
17+
for item in sb.find_elements("ul.products a img"):
18+
if item:
19+
item.flash(color="44CC88")
20+
title = item.get_attribute("title")
21+
if title and required_text in title:
22+
print("* " + title)
23+
sb.sleep(0.1)
24+
sb.sleep(1)
25+
sb.driver.stop()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from seleniumbase import sb_cdp
2+
3+
url = "https://www.nordstrom.com/"
4+
sb = sb_cdp.Chrome(url, locale="en", guest=True)
5+
sb.sleep(2.2)
6+
sb.click("input#keyword-search-input")
7+
sb.sleep(0.8)
8+
search = "cocktail dresses for women teal"
9+
sb.press_keys("input#keyword-search-input", search + "\n")
10+
sb.sleep(2.2)
11+
for i in range(17):
12+
sb.scroll_down(16)
13+
sb.sleep(0.14)
14+
print('*** Nordstrom Search for "%s":' % search)
15+
unique_item_text = []
16+
items = sb.find_elements("article")
17+
for item in items:
18+
description = item.querySelector("article h3")
19+
if description and description.text not in unique_item_text:
20+
unique_item_text.append(description.text)
21+
price_text = ""
22+
price = item.querySelector('div div span[aria-hidden="true"]')
23+
if price:
24+
price_text = price.text
25+
print("* %s (%s)" % (description.text, price_text))
26+
sb.driver.stop()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from seleniumbase import sb_cdp
2+
3+
url = "https://www.walmart.com/"
4+
sb = sb_cdp.Chrome(url, locale="en", guest=True)
5+
sb.sleep(3)
6+
sb.click('input[aria-label="Search"]')
7+
sb.sleep(1.4)
8+
search = "Settlers of Catan Board Game"
9+
required_text = "Catan"
10+
sb.press_keys('input[aria-label="Search"]', search + "\n")
11+
sb.sleep(3.8)
12+
if sb.is_element_visible("#px-captcha"):
13+
sb.gui_click_and_hold("#px-captcha", 7.2)
14+
sb.sleep(4.2)
15+
if sb.is_element_visible("#px-captcha"):
16+
sb.gui_click_and_hold("#px-captcha", 4.2)
17+
sb.sleep(3.2)
18+
sb.remove_elements('[data-testid="skyline-ad"]')
19+
sb.remove_elements('[data-testid="sba-container"]')
20+
print('*** Walmart Search for "%s":' % search)
21+
print(' (Results must contain "%s".)' % required_text)
22+
unique_item_text = []
23+
items = sb.find_elements('div[data-testid="list-view"]')
24+
for item in items:
25+
if required_text in item.text:
26+
description = item.querySelector(
27+
'[data-automation-id="product-title"]'
28+
)
29+
if description and description.text not in unique_item_text:
30+
unique_item_text.append(description.text)
31+
print("* " + description.text)
32+
price = item.querySelector(
33+
'[data-automation-id="product-price"]'
34+
)
35+
if price:
36+
price_text = price.text
37+
price_text = price_text.split("current price Now ")[-1]
38+
price_text = price_text.split("current price ")[-1]
39+
price_text = price_text.split(" ")[0]
40+
print(" (" + price_text + ")")
41+
sb.driver.stop()

examples/cdp_mode/raw_copilot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
sb.sleep(3.5)
2424
stop_button = '[data-testid="stop-button"]'
2525
thumbs_up = 'button[data-testid*="-thumbs-up-"]'
26-
sb.wait_for_element_absent(stop_button, timeout=40)
26+
sb.wait_for_element_absent(stop_button, timeout=45)
2727
sb.wait_for_element(thumbs_up, timeout=20)
2828
sb.sleep(0.6)
2929
sb.click('button[data-testid*="scroll-to-bottom"]')

examples/cdp_mode/raw_easyjet.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@
44
url = "https://www.easyjet.com/en/"
55
sb.activate_cdp_mode(url)
66
sb.sleep(2)
7-
sb.cdp.click_if_visible("button#ensCloseBanner")
7+
sb.click_if_visible("button#ensCloseBanner")
88
sb.sleep(1.2)
9-
sb.cdp.click('input[name="from"]')
9+
sb.click('input[name="from"]')
1010
sb.sleep(1.2)
11-
sb.cdp.type('input[name="from"]', "London Gatwick")
11+
sb.type('input[name="from"]', "London Gatwick")
1212
sb.sleep(0.6)
13-
sb.cdp.click_if_visible("button#ensCloseBanner")
13+
sb.click_if_visible("button#ensCloseBanner")
1414
sb.sleep(0.6)
15-
sb.cdp.click('span[data-testid="airport-name"]')
15+
sb.click('span[data-testid="airport-name"]')
1616
sb.sleep(1.2)
17-
sb.cdp.type('input[name="to"]', "Paris")
17+
sb.type('input[name="to"]', "Paris")
1818
sb.sleep(1.2)
19-
sb.cdp.click('span[data-testid="airport-name"]')
19+
sb.click('span[data-testid="airport-name"]')
2020
sb.sleep(1.2)
21-
sb.cdp.click('input[name="when"]')
21+
sb.click('input[name="when"]')
2222
sb.sleep(1.2)
23-
sb.cdp.click('[data-testid="month"]:last-of-type [aria-disabled="false"]')
23+
sb.click('[data-testid="month"]:last-of-type [aria-disabled="false"]')
2424
sb.sleep(1.2)
25-
sb.cdp.click('[data-testid="month"]:last-of-type [aria-disabled="false"]')
25+
sb.click('[data-testid="month"]:last-of-type [aria-disabled="false"]')
2626
sb.sleep(1.2)
27-
sb.cdp.click('button[data-testid="submit"]')
27+
sb.click('button[data-testid="submit"]')
2828
sb.sleep(4.2)
2929
sb.connect()
3030
sb.sleep(1.2)

examples/cdp_mode/raw_kohls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
44
url = "https://www.kohls.com/"
55
sb.activate_cdp_mode(url)
6-
sb.sleep(2.5)
6+
sb.sleep(2.6)
77
search = "Mickey Mouse 100 friends teal pillow"
88
required_text = "Mickey"
99
sb.cdp.press_keys('input[name="search"]', search + "\n")

0 commit comments

Comments
 (0)