Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/cdp_mode/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ sb.cdp.clear_input(selector, timeout=None)
sb.cdp.clear(selector, timeout=None)
sb.cdp.submit(selector)
sb.cdp.evaluate(expression)
sb.cdp.execute_script(expression)
sb.cdp.js_dumps(obj_name)
sb.cdp.maximize()
sb.cdp.minimize()
Expand Down Expand Up @@ -464,6 +465,9 @@ sb.cdp.get_attribute(selector, attribute)
sb.cdp.get_element_html(selector)
sb.cdp.get_mfa_code(totp_key=None)
sb.cdp.enter_mfa_code(selector, totp_key=None, timeout=None)
sb.cdp.activate_messenger()
sb.cdp.set_messenger_theme(theme="default", location="default")
sb.cdp.post_message(message, duration=None, pause=True, style="info")
sb.cdp.set_locale(locale)
sb.cdp.set_local_storage_item(key, value)
sb.cdp.set_session_storage_item(key, value)
Expand Down Expand Up @@ -528,6 +532,8 @@ sb.cdp.scroll_to_top()
sb.cdp.scroll_to_bottom()
sb.cdp.scroll_up(amount=25)
sb.cdp.scroll_down(amount=25)
sb.cdp.save_page_source(name, folder=None)
sb.cdp.save_as_html(name, folder=None)
sb.cdp.save_screenshot(name, folder=None, selector=None)
sb.cdp.print_to_pdf(name, folder=None)
sb.cdp.save_as_pdf(name, folder=None)
Expand Down
31 changes: 31 additions & 0 deletions examples/cdp_mode/raw_ad_blocking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import mycdp
from seleniumbase import decorators
from seleniumbase import sb_cdp


async def block_urls(tab):
await tab.send(mycdp.network.enable())
await tab.send(mycdp.network.set_blocked_urls(
urls=[
"*googlesyndication.com*",
"*googletagmanager.com*",
"*google-analytics.com*",
"*amazon-adsystem.com*",
"*adsafeprotected.com*",
"*doubleclick.net*",
"*fastclick.net*",
"*snigelweb.com*",
"*2mdn.net*",
]
))

with decorators.print_runtime("raw_ad_blocking.py"):
sb = sb_cdp.Chrome()
loop = sb.get_event_loop()
loop.run_until_complete(block_urls(sb.get_active_tab()))
sb.open("https://www.w3schools.com/jquery/default.asp")
source = sb.get_page_source()
sb.assert_true("doubleclick.net" not in source)
sb.assert_true("google-analytics.com" not in source)
sb.post_message("Blocking was successful!")
sb.driver.quit()
34 changes: 34 additions & 0 deletions examples/cdp_mode/raw_cdp_copilot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from seleniumbase import sb_cdp

url = "https://copilot.microsoft.com/"
sb = sb_cdp.Chrome(url, locale="en", guest=True)
textarea = "textarea#userInput"
sb.wait_for_element(textarea)
sb.sleep(1.5)
sb.click_if_visible('[aria-label="Dismiss"]')
sb.sleep(0.5)
sb.click('button[data-testid*="chat-mode-"]')
sb.sleep(1.1)
sb.click('button[title="Think Deeper"]')
sb.sleep(1.1)
query = "How to start automating with SeleniumBase?"
sb.press_keys(textarea, query)
sb.sleep(1.1)
sb.click('button[data-testid="submit-button"]')
sb.sleep(2.5)
sb.gui_click_captcha()
sb.sleep(2.5)
sb.gui_click_captcha()
sb.sleep(3.5)
stop_button = '[data-testid="stop-button"]'
thumbs_up = 'button[data-testid*="-thumbs-up-"]'
sb.wait_for_element_absent(stop_button, timeout=45)
sb.wait_for_element(thumbs_up, timeout=20)
sb.sleep(0.6)
sb.click('button[data-testid*="scroll-to-bottom"]')
sb.sleep(2.2)
folder = "downloaded_files"
file_name = "copilot_results.html"
sb.save_as_html(file_name, folder)
print('"./%s/%s" was saved!' % (folder, file_name))
sb.driver.stop()
31 changes: 31 additions & 0 deletions examples/cdp_mode/raw_cdp_hyatt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from seleniumbase import sb_cdp

url = "https://www.hyatt.com/"
sb = sb_cdp.Chrome(url, locale="en", guest=True)
sb.sleep(4.2)
sb.click_if_visible('button[aria-label="Close"]')
sb.click_if_visible("#onetrust-reject-all-handler")
sb.sleep(1)
location = "Anaheim, CA, USA"
sb.type('input[id="search-term"]', location)
sb.sleep(1)
sb.click('li[data-js="suggestion"]')
sb.sleep(1)
sb.click("button.be-button-shop")
sb.sleep(6)
card_info = 'div[data-booking-status="BOOKABLE"] [class*="HotelCard_info"]'
hotels = sb.select_all(card_info)
print("Hyatt Hotels in %s:" % location)
print("(" + sb.get_text("ul.b-color_text-white") + ")")
if len(hotels) == 0:
print("No availability over the selected dates!")
for hotel in hotels:
info = hotel.text.strip()
if "Avg/Night" in info and not info.startswith("Rates from"):
name = info.split(" (")[0].split(" + ")[0].split(" Award Cat")[0]
name = name.split(" Rates from :")[0]
price = "?"
if "Rates from : " in info:
price = info.split("Rates from : ")[1].split(" Avg/Night")[0]
print("* %s => %s" % (name, price))
sb.driver.stop()
25 changes: 25 additions & 0 deletions examples/cdp_mode/raw_cdp_kohls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from seleniumbase import sb_cdp

url = "https://www.kohls.com/"
sb = sb_cdp.Chrome(url, locale="en", guest=True)
sb.sleep(2.8)
search = "Mickey Mouse 100 friends teal pillow"
required_text = "Mickey"
sb.press_keys('input[name="search"]', search + "\n")
sb.sleep(5)
for item in sb.find_elements("div.products-container-right"):
if "Sponsored" in item.text:
item.remove_from_dom()
sb.remove_elements("#tce-sticky-wrapper")
sb.remove_elements("li.sponsored-product")
sb.remove_elements("#tce-dec-ces-3-banner")
print('*** Kohls Search for "%s":' % search)
for item in sb.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)
sb.driver.stop()
26 changes: 26 additions & 0 deletions examples/cdp_mode/raw_cdp_nordstrom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from seleniumbase import sb_cdp

url = "https://www.nordstrom.com/"
sb = sb_cdp.Chrome(url, locale="en", guest=True)
sb.sleep(2.2)
sb.click("input#keyword-search-input")
sb.sleep(0.8)
search = "cocktail dresses for women teal"
sb.press_keys("input#keyword-search-input", search + "\n")
sb.sleep(2.2)
for i in range(17):
sb.scroll_down(16)
sb.sleep(0.14)
print('*** Nordstrom Search for "%s":' % search)
unique_item_text = []
items = sb.find_elements("article")
for item in items:
description = item.querySelector("article h3")
if description and description.text not in unique_item_text:
unique_item_text.append(description.text)
price_text = ""
price = item.querySelector('div div span[aria-hidden="true"]')
if price:
price_text = price.text
print("* %s (%s)" % (description.text, price_text))
sb.driver.stop()
41 changes: 41 additions & 0 deletions examples/cdp_mode/raw_cdp_walmart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from seleniumbase import sb_cdp

url = "https://www.walmart.com/"
sb = sb_cdp.Chrome(url, locale="en", guest=True)
sb.sleep(3)
sb.click('input[aria-label="Search"]')
sb.sleep(1.4)
search = "Settlers of Catan Board Game"
required_text = "Catan"
sb.press_keys('input[aria-label="Search"]', search + "\n")
sb.sleep(3.8)
if sb.is_element_visible("#px-captcha"):
sb.gui_click_and_hold("#px-captcha", 7.2)
sb.sleep(4.2)
if sb.is_element_visible("#px-captcha"):
sb.gui_click_and_hold("#px-captcha", 4.2)
sb.sleep(3.2)
sb.remove_elements('[data-testid="skyline-ad"]')
sb.remove_elements('[data-testid="sba-container"]')
print('*** Walmart Search for "%s":' % search)
print(' (Results must contain "%s".)' % required_text)
unique_item_text = []
items = sb.find_elements('div[data-testid="list-view"]')
for item in items:
if required_text in item.text:
description = item.querySelector(
'[data-automation-id="product-title"]'
)
if description and description.text not in unique_item_text:
unique_item_text.append(description.text)
print("* " + description.text)
price = item.querySelector(
'[data-automation-id="product-price"]'
)
if price:
price_text = price.text
price_text = price_text.split("current price Now ")[-1]
price_text = price_text.split("current price ")[-1]
price_text = price_text.split(" ")[0]
print(" (" + price_text + ")")
sb.driver.stop()
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_copilot.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
sb.sleep(3.5)
stop_button = '[data-testid="stop-button"]'
thumbs_up = 'button[data-testid*="-thumbs-up-"]'
sb.wait_for_element_absent(stop_button, timeout=40)
sb.wait_for_element_absent(stop_button, timeout=45)
sb.wait_for_element(thumbs_up, timeout=20)
sb.sleep(0.6)
sb.click('button[data-testid*="scroll-to-bottom"]')
Expand Down
22 changes: 11 additions & 11 deletions examples/cdp_mode/raw_easyjet.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
url = "https://www.easyjet.com/en/"
sb.activate_cdp_mode(url)
sb.sleep(2)
sb.cdp.click_if_visible("button#ensCloseBanner")
sb.click_if_visible("button#ensCloseBanner")
sb.sleep(1.2)
sb.cdp.click('input[name="from"]')
sb.click('input[name="from"]')
sb.sleep(1.2)
sb.cdp.type('input[name="from"]', "London Gatwick")
sb.type('input[name="from"]', "London Gatwick")
sb.sleep(0.6)
sb.cdp.click_if_visible("button#ensCloseBanner")
sb.click_if_visible("button#ensCloseBanner")
sb.sleep(0.6)
sb.cdp.click('span[data-testid="airport-name"]')
sb.click('span[data-testid="airport-name"]')
sb.sleep(1.2)
sb.cdp.type('input[name="to"]', "Paris")
sb.type('input[name="to"]', "Paris")
sb.sleep(1.2)
sb.cdp.click('span[data-testid="airport-name"]')
sb.click('span[data-testid="airport-name"]')
sb.sleep(1.2)
sb.cdp.click('input[name="when"]')
sb.click('input[name="when"]')
sb.sleep(1.2)
sb.cdp.click('[data-testid="month"]:last-of-type [aria-disabled="false"]')
sb.click('[data-testid="month"]:last-of-type [aria-disabled="false"]')
sb.sleep(1.2)
sb.cdp.click('[data-testid="month"]:last-of-type [aria-disabled="false"]')
sb.click('[data-testid="month"]:last-of-type [aria-disabled="false"]')
sb.sleep(1.2)
sb.cdp.click('button[data-testid="submit"]')
sb.click('button[data-testid="submit"]')
sb.sleep(4.2)
sb.connect()
sb.sleep(1.2)
Expand Down
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_kohls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
url = "https://www.kohls.com/"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
sb.sleep(2.6)
search = "Mickey Mouse 100 friends teal pillow"
required_text = "Mickey"
sb.cdp.press_keys('input[name="search"]', search + "\n")
Expand Down
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_nordstrom.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
search = "cocktail dresses for women teal"
sb.cdp.press_keys("input#keyword-search-input", search + "\n")
sb.sleep(2.2)
for i in range(16):
for i in range(17):
sb.cdp.scroll_down(16)
sb.sleep(0.14)
print('*** Nordstrom Search for "%s":' % search)
Expand Down
10 changes: 5 additions & 5 deletions examples/cdp_mode/raw_united.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
destination = "Orlando, FL"
sb.cdp.gui_click_element(origin_input)
sb.sleep(0.5)
sb.cdp.type(origin_input, origin)
sb.type(origin_input, origin)
sb.sleep(1.2)
sb.cdp.click('strong:contains("%s")' % origin)
sb.click('strong:contains("%s")' % origin)
sb.sleep(1.2)
sb.cdp.gui_click_element(destination_input)
sb.sleep(0.5)
sb.cdp.type(destination_input, destination)
sb.type(destination_input, destination)
sb.sleep(1.2)
sb.cdp.click('strong:contains("%s")' % destination)
sb.click('strong:contains("%s")' % destination)
sb.sleep(1.2)
sb.cdp.click('button[aria-label="Find flights"]')
sb.click('button[aria-label="Find flights"]')
sb.sleep(6)
flights = sb.find_elements('div[class*="CardContainer__block"]')
print("**** Flights from %s to %s ****" % (origin, destination))
Expand Down
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_xpath.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Test that CDP Mode can autodetect and use xpath selectors."""
"""Test that CDP Mode can autodetect and use XPath selectors."""
from seleniumbase import SB

with SB(uc=True, test=True) as sb:
Expand Down
18 changes: 8 additions & 10 deletions examples/test_cdp_ad_blocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,20 @@ def test_cdp_network_blocking(self):
message = "Skipping test if reusing session or not Chromium!"
print(message)
self.skip(message)
self.execute_cdp_cmd("Network.enable", {})
self.execute_cdp_cmd(
'Network.setBlockedURLs', {"urls": [
"Network.setBlockedURLs", {"urls": [
"*googlesyndication.com*",
"*doubleclick.net*",
"*adsafeprotected.com*",
"*2mdn.net*",
"*googletagmanager.com*",
"*google-analytics.com*",
"*amazon-adsystem.com*",
"*adsafeprotected.com*",
"*snigelweb.com*",
"*doubleclick.net*",
"*fastclick.net*",
"*amazon-adsystem.com*",
"*google-analytics.com*",
"*snigelweb.com*",
"*2mdn.net*",
]})
self.execute_cdp_cmd('Network.enable', {})
self.open('https://www.w3schools.com/jquery/default.asp')
self.ad_block()
self.open("https://www.w3schools.com/jquery/default.asp")
source = self.get_page_source()
self.assert_true("doubleclick.net" not in source)
self.assert_true("google-analytics.com" not in source)
Expand Down
8 changes: 4 additions & 4 deletions examples/test_roblox_mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_roblox_mobile_site(self):
self.skip('Use "--mobile" to run this test in Mobile Mode!')
self.open("https://www.roblox.com/")
self.assert_element("#download-the-app-container")
self.assert_text("Roblox for Android", "p.roblox-for-platform")
self.assert_text("Continue in App", "a.primary-link")
self.highlight("p.roblox-for-platform", loops=8)
self.highlight("a.primary-link", loops=8)
self.assert_text("Roblox for Android")
self.assert_text("Continue in App", "a.content-action-emphasis")
self.highlight('span:contains("Roblox for Android")', loops=8)
self.highlight("a.content-action-emphasis", loops=8)
4 changes: 3 additions & 1 deletion help_docs/method_summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,10 @@ self.save_screenshot_to_logs(name=None, selector=None, by="css selector")
self.save_as_pdf_to_logs(name=None)
self.save_data_to_logs(data, file_name=None)
self.append_data_to_logs(data, file_name=None)
self.save_page_source(name, folder=None)
self.save_page_source_to_logs(name=None)
self.save_page_source(name, folder=None)
# Duplicates:
# self.save_as_html(name, folder=None)
self.save_cookies(name="cookies.txt")
self.load_cookies(name="cookies.txt", expiry=False)
self.delete_all_cookies()
Expand Down
4 changes: 2 additions & 2 deletions mkdocs_build/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# mkdocs dependencies for generating the seleniumbase.io website
# Minimum Python version: 3.9 (for generating docs only)
# Minimum Python version: 3.10 (for generating docs only)

regex>=2025.9.18
pymdown-extensions>=10.16.1
pipdeptree>=2.28.0
pipdeptree>=2.29.0
python-dateutil>=2.8.2
Markdown==3.9
click==8.3.0
Expand Down
Loading