Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDP Mode - Patch 12 #3275

Merged
merged 6 commits into from
Nov 18, 2024
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
14 changes: 7 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
# Packages
*.egg
*.egg-info
dist
build
.eggs
eggs
parts
bin
var
sdist
develop-eggs
.installed.cfg
bin
build
dist
lib
lib64
parts
sdist
var
.installed.cfg
__pycache__

# Python3 pyvenv
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<h2><img src="https://seleniumbase.github.io/img/logo6.png" title="SeleniumBase" width="32" /> CHANGELOG</h2>

### See: [SeleniumBase/releases](https://github.com/seleniumbase/SeleniumBase/releases)
## See: [SeleniumBase/releases](https://github.com/seleniumbase/SeleniumBase/releases) 🗂️ 📋

### (For CDP updates, see the [CDP Mode docs](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/ReadMe.md))
37 changes: 22 additions & 15 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 Expand Up @@ -395,12 +397,17 @@ sb.cdp.uncheck_if_checked(selector)
sb.cdp.unselect_if_selected(selector)
sb.cdp.is_element_present(selector)
sb.cdp.is_element_visible(selector)
sb.cdp.assert_element_present(selector)
sb.cdp.assert_element_absent(selector)
sb.cdp.wait_for_element_visible(selector)
sb.cdp.assert_element(selector)
sb.cdp.assert_element_visible(selector)
sb.cdp.assert_element_present(selector)
sb.cdp.assert_element_absent(selector)
sb.cdp.assert_element_not_visible(selector)
sb.cdp.assert_element_attribute(selector, attribute, value=None)
sb.cdp.assert_title(title)
sb.cdp.assert_title_contains(substring)
sb.cdp.assert_url(url)
sb.cdp.assert_url_contains(substring)
sb.cdp.assert_text(text, selector="html")
sb.cdp.assert_exact_text(text, selector="html")
sb.cdp.scroll_into_view(selector)
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
Loading