Skip to content

CDP Mode: Patch 38 #3573

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

Merged
merged 5 commits into from
Feb 26, 2025
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ with SB(test=True, uc=True) 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="en") as sb:
url = "https://gitlab.com/users/sign_in"
sb.activate_cdp_mode(url)
sb.uc_gui_click_captcha()
Expand Down
12 changes: 6 additions & 6 deletions examples/cdp_mode/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Simple example: ([SeleniumBase/examples/cdp_mode/raw_gitlab.py](https://github.c
```python
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale="en") as sb:
url = "https://gitlab.com/users/sign_in"
sb.activate_cdp_mode(url)
sb.uc_gui_click_captcha()
Expand Down Expand Up @@ -130,7 +130,7 @@ To find out if WebDriver is connected or disconnected, call:
```python
from seleniumbase import SB

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

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

with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
with SB(uc=True, test=True, locale="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 @@ -328,11 +328,11 @@ with SB(uc=True, test=True, ad_block=True) as sb:
```python
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en", pls="none") as sb:
with SB(uc=True, test=True, locale="en", pls="none") as sb:
url = "https://www.nike.com/"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
sb.cdp.mouse_click('div[data-testid="user-tools-container"]')
sb.cdp.click('div[data-testid="user-tools-container"]')
sb.sleep(1.5)
search = "Nike Air Force 1"
sb.cdp.press_keys('input[type="search"]', search)
Expand Down
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_ahrefs.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, incognito=True, locale_code="en") as 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")'
Expand Down
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_albertsons.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="en") as sb:
url = "https://www.albertsons.com/recipes/"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
Expand Down
20 changes: 8 additions & 12 deletions examples/cdp_mode/raw_async.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import asyncio
import time
from contextlib import suppress
from seleniumbase.core import sb_cdp
from seleniumbase.undetected import cdp_driver
from seleniumbase import sb_cdp
from seleniumbase import cdp_driver


async def main():
driver = await cdp_driver.cdp_util.start_async()
page = await driver.get("about:blank")
await page.set_locale("en")
await page.get("https://www.priceline.com/")
url = "https://www.priceline.com/"
driver = await cdp_driver.start_async(lang="en")
page = await driver.get(url)
time.sleep(3)
print(await page.evaluate("document.title"))
element = await page.select('[data-testid*="endLocation"]')
Expand All @@ -24,7 +23,7 @@ async def main():
loop.run_until_complete(main())

# Call everything without using async / await
driver = cdp_driver.cdp_util.start_sync()
driver = cdp_driver.start_sync()
page = loop.run_until_complete(driver.get("about:blank"))
loop.run_until_complete(page.set_locale("en"))
loop.run_until_complete(page.get("https://www.pokemon.com/us"))
Expand All @@ -41,11 +40,8 @@ async def main():
print(loop.run_until_complete(page.evaluate("document.title")))
time.sleep(1)

# Call CDP methods via the simplified CDP API
page = loop.run_until_complete(driver.get("about:blank"))
sb = sb_cdp.CDPMethods(loop, page, driver)
sb.set_locale("en")
sb.open("https://www.priceline.com/")
# Call CDP methods via the simplified SB CDP API
sb = sb_cdp.Chrome("https://www.priceline.com/")
sb.sleep(2.5)
sb.internalize_links() # Don't open links in a new tab
sb.click("#link_header_nav_experiences")
Expand Down
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", ad_block=True) as sb:
with SB(uc=True, test=True, locale="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
14 changes: 3 additions & 11 deletions examples/cdp_mode/raw_cdp.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
"""Example of using CDP Mode without WebDriver"""
import asyncio
from seleniumbase import decorators
from seleniumbase.core import sb_cdp
from seleniumbase.undetected import cdp_driver
from seleniumbase import sb_cdp


@decorators.print_runtime("CDP Priceline Example")
def main():
url0 = "about:blank" # Set Locale code from here first
url1 = "https://www.priceline.com/" # (The "real" URL)
loop = asyncio.new_event_loop()
driver = cdp_driver.cdp_util.start_sync()
page = loop.run_until_complete(driver.get(url0))
sb = sb_cdp.CDPMethods(loop, page, driver)
sb.set_locale("en") # This test expects English locale
sb.open(url1)
url = "https://www.priceline.com/"
sb = sb_cdp.Chrome(url, lang="en")
sb.sleep(2.5)
sb.internalize_links() # Don't open links in a new tab
sb.click("#link_header_nav_experiences")
Expand Down
23 changes: 23 additions & 0 deletions examples/cdp_mode/raw_cdp_extended.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""The long way of using CDP Mode without WebDriver"""
import asyncio
from seleniumbase import sb_cdp
from seleniumbase import cdp_driver

url = "https://seleniumbase.io/demo_page"
loop = asyncio.new_event_loop()
driver = cdp_driver.start_sync()
page = loop.run_until_complete(driver.get(url))
sb = sb_cdp.CDPMethods(loop, page, driver)

sb.press_keys("input", "Text")
sb.highlight("button")
sb.type("textarea", "Here are some words")
sb.click("button")
sb.set_value("input#mySlider", "100")
sb.click_visible_elements("input.checkBoxClassB")
sb.select_option_by_text("#mySelect", "Set to 75%")
sb.gui_hover_and_click("#myDropdown", "#dropOption2")
sb.gui_click_element("#checkBox1")
sb.gui_drag_and_drop("img#logo", "div#drop2")
sb.nested_click("iframe#myFrame3", ".fBox")
sb.sleep(2)
10 changes: 2 additions & 8 deletions examples/cdp_mode/raw_cdp_methods.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import asyncio
from seleniumbase.core import sb_cdp
from seleniumbase.undetected import cdp_driver
from seleniumbase import sb_cdp

url = "https://seleniumbase.io/demo_page"
loop = asyncio.new_event_loop()
driver = cdp_driver.cdp_util.start_sync()
page = loop.run_until_complete(driver.get(url))
sb = sb_cdp.CDPMethods(loop, page, driver)

sb = sb_cdp.Chrome(url)
sb.press_keys("input", "Text")
sb.highlight("button")
sb.type("textarea", "Here are some words")
Expand Down
13 changes: 3 additions & 10 deletions examples/cdp_mode/raw_cdp_nike.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import asyncio
from seleniumbase.core import sb_cdp
from seleniumbase.undetected import cdp_driver
from seleniumbase import sb_cdp

url = "https://www.nike.com/"
loop = asyncio.new_event_loop()
driver = cdp_driver.cdp_util.start_sync()
page = loop.run_until_complete(driver.get(url))
sb = sb_cdp.CDPMethods(loop, page, driver)

search = "Road Racing Shoes"
sb = sb_cdp.Chrome(url)
sb.click('div[data-testid="user-tools-container"]')
sb.sleep(1)
search = "Road Racing Shoes"
sb.press_keys('input[type="search"]', search)
sb.sleep(4)

elements = sb.select_all('ul[data-testid*="products"] figure .details')
if elements:
print('**** Found results for "%s": ****' % search)
Expand Down
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_cdp_with_sb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from seleniumbase import SB


with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale="en") as sb:
url = "https://www.priceline.com/"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
Expand Down
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_cf.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""Using CDP Mode with PyAutoGUI to bypass CAPTCHAs."""
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en", guest=True) as sb:
with SB(uc=True, test=True, locale="en", guest=True) as sb:
url = "https://www.cloudflare.com/login"
sb.activate_cdp_mode(url)
sb.sleep(3)
sb.uc_gui_handle_captcha() # PyAutoGUI press Tab and Spacebar
sb.sleep(2)

with SB(uc=True, test=True, locale_code="en", guest=True) as sb:
with SB(uc=True, test=True, locale="en", guest=True) as sb:
url = "https://www.cloudflare.com/login"
sb.activate_cdp_mode(url)
sb.sleep(3)
Expand Down
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_easyjet.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", ad_block=True) as sb:
with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
url = "https://www.easyjet.com/en/"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
Expand Down
24 changes: 13 additions & 11 deletions examples/cdp_mode/raw_elal.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="en") as sb:
url = "www.elal.com/flight-deals/en-us/flights-from-boston-to-tel-aviv"
sb.activate_cdp_mode(url)
sb.sleep(2)
Expand Down Expand Up @@ -36,19 +36,21 @@
sb.cdp.click(search_cell)
sb.sleep(5)
else:
elements = sb.cdp.find_elements("div.ui-bound__price__value")
print("*** Lowest Prices: ***")
first = True
departure_prices = "#uiFlightPanel0 div.ui-bound__price__value"
return_prices = "#uiFlightPanel1 div.ui-bound__price__value"
elements = sb.cdp.find_elements(departure_prices)
for element in elements:
if "lowest price" in element.text:
if first:
print("Departure Flight:")
print(element.text)
first = False
else:
print("Return Flight:")
print(element.text)
break
print("Departure Flight:")
print(element.text)
break
elements = sb.cdp.find_elements(return_prices)
for element in elements:
if "lowest price" in element.text:
print("Return Flight:")
print(element.text)
break
dates = sb.cdp.find_elements('div[class*="flight-date"]')
if len(dates) == 2:
print("*** Departure Date: ***")
Expand Down
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_footlocker.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", ad_block=True) as sb:
with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
url = "https://www.footlocker.com/"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
Expand Down
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_gettyimages.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", pls="none") as sb:
with SB(uc=True, test=True, locale="en", pls="none") as sb:
sb.activate_cdp_mode("https://www.gettyimages.com/")
sb.cdp.click('label:contains("Editorial")')
sb.cdp.press_keys("form input", "comic con 2024 sci fi panel\n")
Expand Down
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_gitlab.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="en") as sb:
url = "https://gitlab.com/users/sign_in"
sb.activate_cdp_mode(url)
sb.uc_gui_click_captcha()
Expand Down
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_hyatt.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", ad_block=True) as sb:
with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
url = "https://www.hyatt.com/"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
Expand Down
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_kohls.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", ad_block=True) as sb:
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)
Expand Down
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_multi_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import asyncio
from concurrent.futures import ThreadPoolExecutor
from random import randint
from seleniumbase.undetected import cdp_driver
from seleniumbase import cdp_driver


async def main(url):
driver = await cdp_driver.cdp_util.start_async()
driver = await cdp_driver.start_async()
page = await driver.get(url)
await page.set_window_rect(randint(4, 600), randint(8, 410), 860, 500)
await page.sleep(0.5)
Expand Down
9 changes: 2 additions & 7 deletions examples/cdp_mode/raw_multi_cdp.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
# Testing multiple CDP drivers using the sync API
import asyncio
from concurrent.futures import ThreadPoolExecutor
from random import randint
from seleniumbase.core import sb_cdp
from seleniumbase.undetected import cdp_driver
from seleniumbase import sb_cdp


def main(url):
loop = asyncio.new_event_loop()
driver = cdp_driver.cdp_util.start_sync()
page = loop.run_until_complete(driver.get(url))
sb = sb_cdp.CDPMethods(loop, page, driver)
sb = sb_cdp.Chrome(url)
sb.set_window_rect(randint(4, 720), randint(8, 410), 800, 500)
sb.press_keys("input", "Text")
sb.highlight("button")
Expand Down
4 changes: 2 additions & 2 deletions examples/cdp_mode/raw_nike.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en", pls="none") as sb:
with SB(uc=True, test=True, locale="en", pls="none") as sb:
url = "https://www.nike.com/"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
sb.cdp.mouse_click('div[data-testid="user-tools-container"]')
sb.cdp.click('div[data-testid="user-tools-container"]')
sb.sleep(1.5)
search = "Nike Air Force 1"
sb.cdp.press_keys('input[type="search"]', search)
Expand Down
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_nordstrom.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="en") as sb:
url = "https://www.nordstrom.com/"
sb.activate_cdp_mode(url)
sb.sleep(2.2)
Expand Down
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_pokemon.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", ad_block=True) as sb:
with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
url = "https://www.pokemon.com/us"
sb.activate_cdp_mode(url)
sb.sleep(3.2)
Expand Down
2 changes: 1 addition & 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", ad_block=True) as sb:
with SB(uc=True, test=True, locale="en", ad_block=True) as sb:
window_handle = sb.driver.current_window_handle
url = "https://www.priceline.com"
sb.activate_cdp_mode(url)
Expand Down
Loading