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
636 changes: 323 additions & 313 deletions examples/cdp_mode/ReadMe.md

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions examples/cdp_mode/raw_ahrefs.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from seleniumbase import SB

with SB(uc=True, test=True, incognito=True, locale="en") as sb:
url = "https://ahrefs.com/website-authority-checker"
input_field = 'input[placeholder="Enter domain"]'
submit_button = 'span:contains("Check Authority")'
sb.activate_cdp_mode(url) # The bot-check is later
sb.type(input_field, "github.com/seleniumbase/SeleniumBase")
sb.activate_cdp_mode()
sb.goto("https://ahrefs.com/website-authority-checker")
search_term = "github.com/seleniumbase/SeleniumBase"
sb.type('input[placeholder="Enter domain"]', search_term)
sb.scroll_down(36)
sb.click(submit_button)
sb.click('span:contains("Check Authority")')
sb.sleep(2)
sb.solve_captcha()
sb.sleep(3)
Expand Down
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_albertsons.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from seleniumbase import SB

with SB(uc=True, test=True, locale="en") as sb:
url = "https://www.albertsons.com/recipes/"
sb.activate_cdp_mode(url)
sb.activate_cdp_mode()
sb.goto("https://www.albertsons.com/recipes/")
sb.sleep(2.5)
sb.remove_element("div > div > article")
sb.scroll_into_view('input[type="search"]')
Expand Down
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_amazon.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from seleniumbase import SB

with SB(uc=True, test=True, ad_block=True) as sb:
url = "https://www.amazon.com"
sb.activate_cdp_mode(url)
sb.activate_cdp_mode()
sb.goto("https://www.amazon.com")
sb.sleep(2)
sb.click_if_visible('button[alt="Continue shopping"]')
sb.sleep(2)
Expand Down
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_antibot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from seleniumbase import SB

with SB(uc=True, test=True) as sb:
url = "https://seleniumbase.io/antibot/login"
sb.activate_cdp_mode(url)
sb.activate_cdp_mode()
sb.goto("https://seleniumbase.io/antibot/login")
sb.press_keys("input#username", "demo_user")
sb.type("input#password", "secret_pass")
sb.click("button#myButton")
Expand Down
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ async def main():
cards = sb.select_all('span[data-automation*="product-list-card"]')
for card in cards:
print("* %s" % card.text)
sb.driver.stop()
sb.quit()
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_basic_mobile.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from seleniumbase import SB

with SB(uc=True, test=True, mobile=True) as sb:
url = "https://gitlab.com/users/sign_in"
sb.activate_cdp_mode(url)
sb.activate_cdp_mode()
sb.goto("https://gitlab.com/users/sign_in")
sb.sleep(2)
sb.solve_captcha()
# (The rest is for testing and demo purposes)
Expand Down
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_bestwestern.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from seleniumbase import SB

with SB(uc=True, test=True, locale="en", guest=True) as sb:
url = "https://www.bestwestern.com/en_US.html"
sb.activate_cdp_mode(url)
sb.activate_cdp_mode()
sb.goto("https://www.bestwestern.com/en_US.html")
sb.sleep(3)
sb.click_if_visible(".onetrust-close-btn-handler")
sb.sleep(1)
Expand Down
8 changes: 4 additions & 4 deletions examples/cdp_mode/raw_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def get_canvas_pixel_colors_at_top_left(sb):

with SB(uc=True, test=True) as sb:
# Testing click_with_offset()
url = "https://seleniumbase.io/canvas/"
sb.activate_cdp_mode(url)
sb.activate_cdp_mode()
sb.goto("https://seleniumbase.io/canvas/")
sb.assert_title_contains("Canvas")
sb.highlight("canvas")
rgb = get_canvas_pixel_colors_at_top_left(sb)
Expand All @@ -26,8 +26,8 @@ def get_canvas_pixel_colors_at_top_left(sb):

with SB(uc=True, test=True) as sb:
# Testing gui_click_with_offset()
url = "https://seleniumbase.io/other/canvas"
sb.activate_cdp_mode(url)
sb.activate_cdp_mode()
sb.goto("https://seleniumbase.io/other/canvas")
sb.assert_title_contains("Canvas")
sb.click_with_offset("canvas", 0, 0, center=True)
sb.sleep(1)
Expand Down
6 changes: 3 additions & 3 deletions examples/cdp_mode/raw_cdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

@decorators.print_runtime("CDP Priceline Example")
def main():
url = "https://www.priceline.com/"
sb = sb_cdp.Chrome(url, lang="en")
sb = sb_cdp.Chrome(lang="en")
sb.goto("https://www.priceline.com/")
sb.sleep(2)
sb.internalize_links() # Don't open links in a new tab
sb.click("#link_header_nav_experiences")
Expand All @@ -32,7 +32,7 @@ def main():
cards = sb.select_all('span[data-automation*="product-list-card"]')
for card in cards:
print("* %s" % card.text)
sb.driver.stop()
sb.quit()


if __name__ == "__main__":
Expand Down
19 changes: 9 additions & 10 deletions examples/cdp_mode/raw_cdp_copilot.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
from contextlib import suppress
from seleniumbase import sb_cdp

url = "https://copilot.microsoft.com/"
sb = sb_cdp.Chrome(url, locale="en", guest=True)
sb = sb_cdp.Chrome(locale="en", incognito=True)
sb.goto("https://copilot.microsoft.com/")
textarea = "textarea#userInput"
sb.wait_for_element(textarea)
sb.sleep(1.3)
sb.click_if_visible('[aria-label="Dismiss"]')
sb.sleep(0.5)
sb.click('button[data-testid*="chat-mode-"]')
sb.sleep(1.1)
sb.click_if_visible('button[title^="Think"]')
sb.sleep(1.1)
sb.sleep(1.2)
query = "How to start automating with SeleniumBase?"
sb.press_keys(textarea, query)
sb.sleep(1.1)
seen_text = sb.get_text(textarea)
if seen_text != query and seen_text in query:
# When CAPTCHA appears while typing text
sb.sleep(1.1)
sb.sleep(1.2)
sb.solve_captcha()
sb.sleep(2.2)
sb.type(textarea, "")
Expand All @@ -31,8 +28,10 @@
sb.sleep(2.5)
stop_button = '[data-testid="stop-button"]'
thumbs_up = 'button[data-testid*="-thumbs-up-"]'
sb.wait_for_element_absent(stop_button, timeout=50)
sb.wait_for_element(thumbs_up, timeout=20)
with suppress(Exception):
sb.wait_for_element_absent(stop_button, timeout=45)
with suppress(Exception):
sb.wait_for_element(thumbs_up, timeout=15)
sb.sleep(0.6)
scroll = 'button[data-testid*="scroll-to-bottom"]'
sb.click_if_visible(scroll)
Expand Down
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_cdp_gitlab.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from seleniumbase import sb_cdp

url = "https://gitlab.com/users/sign_in"
sb = sb_cdp.Chrome(url, incognito=True)
sb = sb_cdp.Chrome(incognito=True)
sb.goto("https://gitlab.com/users/sign_in")
sb.sleep(2)
sb.solve_captcha()
sb.highlight('h1:contains("GitLab")')
Expand Down
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_cdp_hyatt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from seleniumbase import sb_cdp

url = "https://www.hyatt.com/"
sb = sb_cdp.Chrome(url, locale="en", guest=True)
sb = sb_cdp.Chrome(locale="en", guest=True)
sb.goto("https://www.hyatt.com/")
sb.sleep(3.6)
sb.click_if_visible('button[aria-label="Close"]')
sb.sleep(0.1)
Expand Down
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_cdp_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@


def main():
url = "https://seleniumbase.io/simple/login"
sb = sb_cdp.Chrome(url)
sb = sb_cdp.Chrome()
sb.goto("https://seleniumbase.io/simple/login")
sb.type("#username", "demo_user")
sb.type("#password", "secret_pass")
sb.click('a:contains("Sign in")')
Expand Down
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_cdp_methods.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from seleniumbase import sb_cdp

url = "https://seleniumbase.io/demo_page"
sb = sb_cdp.Chrome(url)
sb = sb_cdp.Chrome()
sb.goto("https://seleniumbase.io/demo_page")
sb.press_keys("input", "Text")
sb.highlight("button")
sb.type("textarea", "Here are some words")
Expand Down
3 changes: 1 addition & 2 deletions examples/cdp_mode/raw_cdp_mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
)
)
)
url = "https://gitlab.com/users/sign_in"
sb.goto(url)
sb.goto("https://gitlab.com/users/sign_in")
sb.sleep(2)
sb.solve_captcha()
# (The rest is for testing and demo purposes)
Expand Down
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_cdp_nike.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from seleniumbase import sb_cdp

url = "https://www.nike.com/"
sb = sb_cdp.Chrome(url)
sb = sb_cdp.Chrome()
sb.goto("https://www.nike.com/")
sb.sleep(1.2)
sb.click('[data-testid="user-tools-container"] search')
sb.sleep(1)
Expand Down
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_cdp_nordstrom.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from seleniumbase import sb_cdp

url = "https://www.nordstrom.com/"
sb = sb_cdp.Chrome(url, locale="en", guest=True)
sb = sb_cdp.Chrome(locale="en", guest=True)
sb.goto("https://www.nordstrom.com/")
sb.sleep(2.2)
sb.click("input#keyword-search-input")
sb.sleep(0.8)
Expand Down
5 changes: 3 additions & 2 deletions examples/cdp_mode/raw_cdp_patent.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from seleniumbase import sb_cdp

url = "https://www.lens.org/lens/patent/135-034-272-112-366/frontpage"
sb = sb_cdp.Chrome(url)
sb = sb_cdp.Chrome()
sb.goto("https://www.lens.org/lens/patent/135-034-272-112-366/frontpage")
sb.sleep(4)
sb.solve_captcha()
sb.flash('[ng-if*="patent.title"]', duration=3, pause=2)
print("* " + sb.get_text('[ng-if*="patent.title"]') + " *")
print(sb.get_text("ol.claims"))
sb.quit()
3 changes: 2 additions & 1 deletion examples/cdp_mode/raw_cdp_reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

search = "reddit+scraper"
url = f"https://www.reddit.com/r/webscraping/search/?q={search}"
sb = sb_cdp.Chrome(url, use_chromium=True)
sb = sb_cdp.Chrome(use_chromium=True)
sb.goto(url)
sb.solve_captcha() # Might not be needed
post_title = '[data-testid="post-title"]'
sb.wait_for_element(post_title)
Expand Down
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_cdp_shadow.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""An example of displaying Shadow DOM inside HTML"""
from seleniumbase import sb_cdp

url = "https://seleniumbase.io/apps/turnstile"
sb = sb_cdp.Chrome(url)
sb = sb_cdp.Chrome()
sb.goto("https://seleniumbase.io/apps/turnstile")
element = sb.find_element("div.cf-turnstile div")
html_with_shadow_dom = element.get_html()
print(html_with_shadow_dom)
Expand Down
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_cdp_turnstile.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from seleniumbase import sb_cdp

url = "https://seleniumbase.io/apps/turnstile"
sb = sb_cdp.Chrome(url)
sb = sb_cdp.Chrome()
sb.goto("https://seleniumbase.io/apps/turnstile")
sb.solve_captcha()
sb.assert_element("img#captcha-success")
sb.set_messenger_theme(location="top_left")
Expand Down
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_cdp_walmart.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from seleniumbase import sb_cdp

url = "https://www.walmart.com/"
sb = sb_cdp.Chrome(url, locale="en", guest=True)
sb = sb_cdp.Chrome(locale="en", guest=True)
sb.goto("https://www.walmart.com/")
sb.sleep(3)
continue_button = 'button:contains("Continue shopping")'
if sb.is_element_visible(continue_button):
Expand Down
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_cdp_with_sb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@


with SB(uc=True, test=True, locale="en") as sb:
url = "https://www.priceline.com/"
sb.activate_cdp_mode(url)
sb.activate_cdp_mode()
sb.goto("https://www.priceline.com/")
sb.sleep(2.5)
sb.internalize_links() # Don't open links in a new tab
sb.click_if_visible('[aria-label="Close Modal"]')
Expand Down
15 changes: 15 additions & 0 deletions examples/cdp_mode/raw_cdp_xpath.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Test that CDP Mode can use XPath selectors."""
from seleniumbase import sb_cdp

sb = sb_cdp.Chrome()
sb.goto("https://seleniumbase.io/demo_page")
sb.highlight('//input[@id="myTextInput"]')
sb.type('//*[@id="myTextInput"]', "XPath Test!")
sb.sleep(0.5)
sb.highlight('//button[contains(text(),"(Green)")]')
sb.click('//button[starts-with(text(),"Click Me")]')
sb.assert_element('//button[contains(., "Purple")]')
sb.sleep(0.5)
sb.highlight("//table/tbody/tr/td/h3")
sb.highlight("//table/tbody/tr[1]/td[2]/h2")
sb.assert_text("SeleniumBase", "//table//h2")
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_cdp_yc_news.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from seleniumbase import sb_cdp

url = "https://news.ycombinator.com/submitted?id=seleniumbase"
sb = sb_cdp.Chrome(url)
sb = sb_cdp.Chrome()
sb.goto("https://news.ycombinator.com/submitted?id=seleniumbase")
elements = sb.find_elements("span.titleline > a")
for element in elements:
print("* " + element.text)
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_cf_captcha.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from seleniumbase import SB

with SB(uc=True, test=True, incognito=True) as sb:
url = "https://www.cloudflare.com/login"
sb.activate_cdp_mode(url)
sb.activate_cdp_mode()
sb.goto("https://www.cloudflare.com/login")
sb.wait_for_element('div[data-testid*="challenge-widget"]')
sb.sleep(2.5)
sb.solve_captcha()
Expand Down
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_cf_clearance.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def get_cf_clearance_cookie(sb):
return None


url = "https://gitlab.com/users/sign_in"
sb = sb_cdp.Chrome(url)
sb = sb_cdp.Chrome()
sb.goto("https://gitlab.com/users/sign_in")
sb.sleep(3) # Wait for CAPTCHA to load
sb.solve_captcha() # (Only if found)
sb.sleep(2) # Wait for CAPTCHA success
Expand Down
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from seleniumbase import SB

with SB(uc=True, test=True, ad_block=True) as sb:
url = "https://chatgpt.com/"
sb.activate_cdp_mode(url)
sb.activate_cdp_mode()
sb.open("https://chatgpt.com/")
sb.sleep(1)
sb.click_if_visible('button[aria-label="Close dialog"]')
sb.click_if_visible('button[data-testid="close-button"]')
Expand Down
19 changes: 9 additions & 10 deletions examples/cdp_mode/raw_copilot.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
from contextlib import suppress
from seleniumbase import SB

with SB(uc=True, test=True, locale="en") as sb:
url = "https://copilot.microsoft.com/"
sb.activate_cdp_mode(url)
with SB(uc=True, test=True, locale="en", incognito=True) as sb:
sb.activate_cdp_mode()
sb.goto("https://copilot.microsoft.com/")
textarea = "textarea#userInput"
sb.wait_for_element(textarea)
sb.sleep(1.3)
sb.click_if_visible('[aria-label="Dismiss"]')
sb.sleep(0.5)
sb.click('button[data-testid*="chat-mode-"]')
sb.sleep(1.1)
sb.click_if_visible('button[title^="Think"]')
sb.sleep(1.1)
sb.sleep(1.2)
query = "How to start automating with SeleniumBase?"
sb.press_keys(textarea, query)
sb.sleep(1.1)
Expand All @@ -32,8 +29,10 @@
sb.sleep(2.5)
stop_button = '[data-testid="stop-button"]'
thumbs_up = 'button[data-testid*="-thumbs-up-"]'
sb.wait_for_element_absent(stop_button, timeout=50)
sb.wait_for_element(thumbs_up, timeout=20)
with suppress(Exception):
sb.wait_for_element_absent(stop_button, timeout=45)
with suppress(Exception):
sb.wait_for_element(thumbs_up, timeout=15)
sb.sleep(0.6)
scroll = 'button[data-testid*="scroll-to-bottom"]'
sb.click_if_visible(scroll)
Expand Down
Loading