Skip to content

Commit

Permalink
Update CDP Mode examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmintz committed Nov 18, 2024
1 parent ff2b0dc commit 273d4b2
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 91 deletions.
28 changes: 15 additions & 13 deletions examples/cdp_mode/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,21 @@ To find out if WebDriver is connected or disconnected, call:
```python
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
url = "https://www.pokemon.com/us"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
sb.cdp.click_if_visible("button#onetrust-reject-all-handler")
sb.sleep(1.2)
sb.cdp.click('a[href="https://www.pokemon.com/us/pokedex/"]')
sb.sleep(3.2)
sb.cdp.click("button#onetrust-accept-btn-handler")
sb.sleep(1.2)
sb.cdp.click("a span.icon_pokeball")
sb.sleep(2.5)
sb.cdp.click('b:contains("Show Advanced Search")')
sb.sleep(1.2)
sb.sleep(2.5)
sb.cdp.click('span[data-type="type"][data-value="electric"]')
sb.sleep(0.5)
sb.scroll_into_view("a#advSearch")
sb.sleep(0.5)
sb.cdp.click("a#advSearch")
sb.cdp.mouse_click("a#advSearch")
sb.sleep(1.2)
sb.cdp.click('img[src*="img/pokedex/detail/025.png"]')
sb.cdp.assert_text("Pikachu", 'div[class*="title"]')
Expand All @@ -102,11 +102,13 @@ with SB(uc=True, test=True, locale_code="en") as sb:
sb.cdp.highlight_overlay("div.pokemon-ability-info")
sb.sleep(2)
sb.cdp.click('a[href="https://www.pokemon.com/us/play-pokemon/"]')
sb.sleep(0.6)
sb.cdp.click('h3:contains("Find an Event")')
location = "Concord, MA, USA"
sb.cdp.type('input[data-testid="location-search"]', location)
sb.sleep(1)
sb.sleep(1.5)
sb.cdp.click("div.autocomplete-dropdown-container div.suggestion-item")
sb.sleep(0.6)
sb.cdp.click('img[alt="search-icon"]')
sb.sleep(2)
events = sb.cdp.select_all('div[data-testid="event-name"]')
Expand All @@ -129,10 +131,10 @@ with SB(uc=True, test=True, locale_code="en") as sb:
```python
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
url = "https://www.hyatt.com/"
sb.activate_cdp_mode(url)
sb.sleep(2)
sb.sleep(2.5)
sb.cdp.click_if_visible('button[aria-label="Close"]')
sb.sleep(1)
sb.cdp.click('span:contains("Explore")')
Expand Down Expand Up @@ -176,7 +178,7 @@ with SB(uc=True, test=True, locale_code="en") as sb:
```python
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
url = "https://www.bestwestern.com/en_US.html"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
Expand Down Expand Up @@ -220,7 +222,7 @@ with SB(uc=True, test=True, locale_code="en") as sb:
```python
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
url = "https://www.walmart.com/"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
Expand Down Expand Up @@ -266,7 +268,7 @@ with SB(uc=True, test=True, locale_code="en") as sb:
```python
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
url = "https://www.nike.com/"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
Expand Down
21 changes: 13 additions & 8 deletions examples/cdp_mode/raw_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async def main():
time.sleep(1)
element = loop.run_until_complete(page.select("span.icon_pokeball"))
loop.run_until_complete(element.click_async())
time.sleep(1.5)
time.sleep(2)
print(loop.run_until_complete(page.evaluate("document.title")))
time.sleep(1)

Expand All @@ -46,17 +46,22 @@ async def main():
sb = sb_cdp.CDPMethods(loop, page, driver)
sb.set_locale("en")
sb.open("https://www.priceline.com/")
sb.sleep(3)
sb.sleep(2.5)
sb.internalize_links() # Don't open links in a new tab
sb.click("#link_header_nav_experiences")
sb.sleep(2.5)
sb.sleep(3.5)
sb.remove_elements("msm-cookie-banner")
sb.sleep(1.5)
location = "Amsterdam"
sb.press_keys('input[data-test-id*="search"]', location)
where_to = 'div[data-automation*="experiences"] input'
button = 'button[data-automation*="experiences-search"]'
sb.gui_click_element(where_to)
sb.press_keys(where_to, location)
sb.sleep(1)
sb.click('input[data-test-id*="search"]')
sb.sleep(2)
sb.click('span[data-test-id*="autocomplete"]')
sb.sleep(5)
sb.gui_click_element(button)
sb.sleep(3)
print(sb.get_title())
print("************")
cards = sb.select_all('h2[data-automation*="product-list-card"]')
for card in cards:
print("* %s" % card.text)
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_bestwestern.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
url = "https://www.bestwestern.com/en_US.html"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
Expand Down
30 changes: 14 additions & 16 deletions examples/cdp_mode/raw_cdp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Example of using CDP Mode without WebDriver"""
import asyncio
from contextlib import suppress
from seleniumbase import decorators
from seleniumbase.core import sb_cdp
from seleniumbase.undetected import cdp_driver
Expand All @@ -16,29 +15,28 @@ def main():
sb = sb_cdp.CDPMethods(loop, page, driver)
sb.set_locale("en") # This test expects English locale
sb.open(url1)
sb.sleep(3)
sb.sleep(2.5)
sb.internalize_links() # Don't open links in a new tab
sb.click("#link_header_nav_experiences")
sb.sleep(2.5)
sb.sleep(3.5)
sb.remove_elements("msm-cookie-banner")
sb.sleep(1.5)
location = "Amsterdam"
sb.press_keys('input[data-test-id*="search"]', location)
where_to = 'div[data-automation*="experiences"] input'
button = 'button[data-automation*="experiences-search"]'
sb.gui_click_element(where_to)
sb.press_keys(where_to, location)
sb.sleep(1)
sb.click('input[data-test-id*="search"]')
sb.sleep(2)
sb.click('span[data-test-id*="autocomplete"]')
sb.sleep(5)
sb.gui_click_element(button)
sb.sleep(3)
print(sb.get_title())
header = sb.get_text('h2[data-testid*="RelatedVenues"]')
print("*** %s: ***" % header)
cards = sb.select_all("div.venue-card__body")
print("************")
for i in range(8):
sb.scroll_down(50)
sb.sleep(0.2)
cards = sb.select_all('h2[data-automation*="product-list-card"]')
for card in cards:
with suppress(Exception):
venue = card.text.split("\n")[0].strip()
rating = card.text.split("\n")[1].strip()
reviews = card.text.split("\n")[2].strip()[1:-1]
print("* %s: %s from %s reviews." % (venue, rating, reviews))
print("* %s" % card.text)


if __name__ == "__main__":
Expand Down
28 changes: 13 additions & 15 deletions examples/cdp_mode/raw_cdp_with_sb.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Example of using CDP Mode with WebDriver"""
from contextlib import suppress
from seleniumbase import SB


Expand All @@ -9,23 +8,22 @@
sb.sleep(2.5)
sb.internalize_links() # Don't open links in a new tab
sb.click("#link_header_nav_experiences")
sb.sleep(2.5)
sb.sleep(3.5)
sb.remove_elements("msm-cookie-banner")
sb.sleep(1.5)
location = "Amsterdam"
sb.press_keys('input[data-test-id*="search"]', location)
where_to = 'div[data-automation*="experiences"] input'
button = 'button[data-automation*="experiences-search"]'
sb.cdp.gui_click_element(where_to)
sb.press_keys(where_to, location)
sb.sleep(1)
sb.click('input[data-test-id*="search"]')
sb.sleep(2)
sb.click('span[data-test-id*="autocomplete"]')
sb.sleep(5)
sb.cdp.gui_click_element(button)
sb.sleep(3)
print(sb.get_title())
header = sb.get_text('h2[data-testid*="RelatedVenues"]')
print("*** %s: ***" % header)
cards = sb.select_all("div.venue-card__body")
print("************")
for i in range(8):
sb.cdp.scroll_down(50)
sb.sleep(0.2)
cards = sb.select_all('h2[data-automation*="product-list-card"]')
for card in cards:
with suppress(Exception):
venue = card.text.split("\n")[0].strip()
rating = card.text.split("\n")[1].strip()
reviews = card.text.split("\n")[2].strip()[1:-1]
print("* %s: %s from %s reviews." % (venue, rating, reviews))
print("* %s" % card.text)
26 changes: 16 additions & 10 deletions examples/cdp_mode/raw_easyjet.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
url = "https://www.easyjet.com/en/"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
sb.cdp.click_if_visible('button#ensRejectAll')
sb.cdp.click_if_visible("button#ensCloseBanner")
sb.sleep(1.2)
sb.cdp.click('input[name="from"]')
sb.sleep(1.2)
sb.cdp.type('input[name="from"]', "London")
sb.sleep(1.2)
sb.sleep(0.6)
sb.cdp.click_if_visible("button#ensCloseBanner")
sb.sleep(0.6)
sb.cdp.click('span[data-testid="airport-name"]')
sb.sleep(1.2)
sb.cdp.type('input[name="to"]', "Venice")
Expand All @@ -23,16 +25,20 @@
sb.cdp.click('[data-testid="month"]:last-of-type [aria-disabled="false"]')
sb.sleep(1.2)
sb.cdp.click('button[data-testid="submit"]')
sb.sleep(3.5)
sb.sleep(2.5)
sb.connect()
sb.sleep(0.5)
if "/buy/flights" not in sb.get_current_url():
sb.driver.close()
sb.switch_to_newest_window()
days = sb.find_elements("div.flight-grid-day")
sb.sleep(1.2)
for window in sb.driver.window_handles:
sb.switch_to_window(window)
if "/buy/flights" in sb.get_current_url():
break
sb.click_if_visible("button#ensCloseBanner")
days = sb.find_elements('div[class*="FlightGridLayout_column"]')
for day in days:
if not day.text.strip():
continue
print("**** " + " ".join(day.text.split("\n")[0:2]) + " ****")
fares = day.find_elements("css selector", "button.selectable")
fares = day.find_elements("css selector", 'button[class*="flightDet"]')
if not fares:
print("No flights today!")
for fare in fares:
Expand Down
10 changes: 5 additions & 5 deletions examples/cdp_mode/raw_footlocker.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
url = "https://www.footlocker.com/"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
sb.cdp.click_if_visible('button[id*="Agree"]')
sb.sleep(1.5)
sb.sleep(2.5)
sb.cdp.mouse_click('input[aria-label="Search"]')
sb.sleep(1.5)
sb.sleep(2.5)
search = "Nike Shoes"
sb.cdp.press_keys('input[aria-label="Search"]', search)
sb.sleep(2)
sb.sleep(2.5)
sb.cdp.mouse_click('ul[id*="typeahead"] li div')
sb.sleep(3)
sb.sleep(3.5)
elements = sb.cdp.select_all("a.ProductCard-link")
if elements:
print('**** Found results for "%s": ****' % search)
Expand Down
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_hyatt.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
url = "https://www.hyatt.com/"
sb.activate_cdp_mode(url)
sb.sleep(2)
sb.sleep(2.5)
sb.cdp.click_if_visible('button[aria-label="Close"]')
sb.sleep(1)
sb.cdp.click('span:contains("Explore")')
Expand Down
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_nike.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
url = "https://www.nike.com/"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
Expand Down
18 changes: 10 additions & 8 deletions examples/cdp_mode/raw_pokemon.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
url = "https://www.pokemon.com/us"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
sb.cdp.click_if_visible("button#onetrust-reject-all-handler")
sb.sleep(1.2)
sb.cdp.click('a[href="https://www.pokemon.com/us/pokedex/"]')
sb.sleep(3.2)
sb.cdp.click("button#onetrust-accept-btn-handler")
sb.sleep(1.2)
sb.cdp.click("a span.icon_pokeball")
sb.sleep(2.5)
sb.cdp.click('b:contains("Show Advanced Search")')
sb.sleep(1.2)
sb.sleep(2.5)
sb.cdp.click('span[data-type="type"][data-value="electric"]')
sb.sleep(0.5)
sb.scroll_into_view("a#advSearch")
sb.sleep(0.5)
sb.cdp.click("a#advSearch")
sb.cdp.mouse_click("a#advSearch")
sb.sleep(1.2)
sb.cdp.click('img[src*="img/pokedex/detail/025.png"]')
sb.cdp.assert_text("Pikachu", 'div[class*="title"]')
Expand All @@ -31,11 +31,13 @@
sb.cdp.highlight_overlay("div.pokemon-ability-info")
sb.sleep(2)
sb.cdp.click('a[href="https://www.pokemon.com/us/play-pokemon/"]')
sb.sleep(0.6)
sb.cdp.click('h3:contains("Find an Event")')
location = "Concord, MA, USA"
sb.cdp.type('input[data-testid="location-search"]', location)
sb.sleep(1)
sb.sleep(1.5)
sb.cdp.click("div.autocomplete-dropdown-container div.suggestion-item")
sb.sleep(0.6)
sb.cdp.click('img[alt="search-icon"]')
sb.sleep(2)
events = sb.cdp.select_all('div[data-testid="event-name"]')
Expand Down
3 changes: 2 additions & 1 deletion examples/cdp_mode/raw_priceline.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
window_handle = sb.driver.current_window_handle
url = "https://www.priceline.com"
sb.activate_cdp_mode(url)
Expand All @@ -24,6 +24,7 @@
sb.sleep(0.2)
sb.switch_to_newest_window()
sb.sleep(0.6)
sb.sleep(0.8)
for y in range(1, 9):
sb.scroll_to_y(y * 400)
sb.sleep(1.25)
Expand Down
Loading

0 comments on commit 273d4b2

Please sign in to comment.