Merge pull request #3573 from seleniumbase/cdp-mode-patch-38

CDP Mode: Patch 38
This commit is contained in:
Michael Mintz 2025-02-26 17:38:55 -05:00 committed by GitHub
commit aa0b70a799
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
54 changed files with 188 additions and 162 deletions

View File

@ -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()

View File

@ -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()
@ -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)
@ -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)
@ -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)
@ -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)

View File

@ -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")'

View File

@ -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)

View File

@ -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"]')
@ -24,7 +23,7 @@ if __name__ == "__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"))
@ -41,11 +40,8 @@ if __name__ == "__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")

View File

@ -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)

View File

@ -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")

View File

@ -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)

View File

@ -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")

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)
@ -36,19 +36,21 @@ with SB(uc=True, test=True, locale_code="en") as sb:
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: ***")

View File

@ -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)

View File

@ -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")

View File

@ -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()

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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")

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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.researchgate.net/search/publication"
sb.activate_cdp_mode(url)
sb.sleep(2)

View File

@ -1,6 +1,6 @@
from seleniumbase import SB
with SB(uc=True, test=True, locale_code="en", incognito=True) as sb:
with SB(uc=True, test=True, locale="en", incognito=True) as sb:
url = "https://www.radwell.com/en-US/Search/Advanced/"
sb.activate_cdp_mode(url)
sb.sleep(3)

View File

@ -4,7 +4,7 @@ import colorama
import mycdp
import sys
from seleniumbase import decorators
from seleniumbase.undetected import cdp_driver
from seleniumbase import cdp_driver
c1 = colorama.Fore.RED + colorama.Back.LIGHTYELLOW_EX
c2 = colorama.Fore.BLUE + colorama.Back.LIGHTCYAN_EX
@ -30,7 +30,7 @@ class RequestPausedTest():
)
async def start_test(self):
driver = await cdp_driver.cdp_util.start_async()
driver = await cdp_driver.start_async()
tab = await driver.get("about:blank")
tab.add_handler(mycdp.fetch.RequestPaused, self.request_paused_handler)
url = "https://gettyimages.com/photos/firefly-2003-nathan"

View File

@ -14,7 +14,7 @@ async def request_paused_handler(event, tab):
tab.feed_cdp(mycdp.fetch.continue_request(request_id=rid, url=new_url))
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("about:blank")
sb.cdp.add_handler(mycdp.fetch.RequestPaused, request_paused_handler)
sb.cdp.open("https://gettyimages.com/photos/jonathan-frakes-cast-2022")

View File

@ -23,7 +23,7 @@ async def request_paused_handler(event, tab):
tab.feed_cdp(mycdp.fetch.fail_request(event.request_id, TIMED_OUT))
with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale="en") as sb:
sb.activate_cdp_mode("about:blank")
sb.cdp.add_handler(mycdp.fetch.RequestPaused, request_paused_handler)
url = "https://gettyimages.com/photos/firefly-2003-nathan"

View File

@ -25,13 +25,13 @@ async def receive_handler(event: mycdp.network.ResponseReceived):
print(event.response)
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.cdp.add_handler(mycdp.network.RequestWillBeSent, send_handler)
sb.cdp.add_handler(mycdp.network.ResponseReceived, receive_handler)
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)

View File

@ -25,7 +25,7 @@ async def receive_handler(event: mycdp.network.ResponseReceived):
print(event.response)
with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale="en") as sb:
sb.activate_cdp_mode("about:blank")
sb.cdp.add_handler(mycdp.network.RequestWillBeSent, send_handler)
sb.cdp.add_handler(mycdp.network.ResponseReceived, receive_handler)

View File

@ -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.southwest.com/air/booking/"
sb.activate_cdp_mode(url)
sb.sleep(2.8)

View File

@ -1,7 +1,7 @@
"""Simple web-scraping example in CDP Mode"""
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://architectureofcities.com/roman-theaters"
sb.activate_cdp_mode(url)
sb.cdp.click_if_visible("#cn-close-notice")

View File

@ -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.united.com/en/us"
sb.activate_cdp_mode(url)
sb.sleep(2.5)

View File

@ -1,6 +1,6 @@
from seleniumbase import SB
with SB(uc=True, test=True, locale_code="en", incognito=True) as sb:
with SB(uc=True, test=True, locale="en", incognito=True) as sb:
url = "https://wsform.com/demo/"
sb.activate_cdp_mode(url)
sb.scroll_into_view("div.grid")

View File

@ -5,7 +5,7 @@ import colorama
import mycdp
import sys
import time
from seleniumbase.undetected import cdp_driver
from seleniumbase import cdp_driver
xhr_requests = []
last_xhr_request = None
@ -58,7 +58,7 @@ async def receiveXHR(page, requests):
async def crawl():
driver = await cdp_driver.cdp_util.start_async()
driver = await cdp_driver.start_async()
tab = await driver.get("about:blank")
listenXHR(tab)

View File

@ -56,7 +56,7 @@ async def receiveXHR(page, requests):
return responses
with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale="en") as sb:
sb.activate_cdp_mode("about:blank")
tab = sb.cdp.page
listenXHR(tab)

View File

@ -273,7 +273,7 @@ class UCPresentationClass(BaseCase):
self.begin_presentation(filename="uc_presentation.html")
with suppress(Exception):
with SB(uc=True, incognito=True, locale_code="en") as sb:
with SB(uc=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")'

View File

@ -352,7 +352,7 @@ class UCPresentationClass(BaseCase):
)
self.begin_presentation(filename="uc_presentation.html")
with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale="en") as sb:
url = "www.planetminecraft.com/account/sign_in/"
sb.activate_cdp_mode(url)
sb.sleep(2)
@ -367,7 +367,7 @@ class UCPresentationClass(BaseCase):
)
self.begin_presentation(filename="uc_presentation.html")
with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale="en") as sb:
url = "https://www.cloudflare.com/login"
sb.activate_cdp_mode(url)
sb.sleep(3)
@ -382,7 +382,7 @@ class UCPresentationClass(BaseCase):
)
self.begin_presentation(filename="uc_presentation.html")
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.sleep(2)
@ -433,7 +433,7 @@ class UCPresentationClass(BaseCase):
)
self.begin_presentation(filename="uc_presentation.html")
with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale="en") as sb:
url = "https://www.bing.com/turing/captcha/challenge"
sb.activate_cdp_mode(url)
sb.sleep(1)
@ -458,7 +458,7 @@ class UCPresentationClass(BaseCase):
)
self.begin_presentation(filename="uc_presentation.html")
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)
@ -565,7 +565,7 @@ class UCPresentationClass(BaseCase):
)
self.begin_presentation(filename="uc_presentation.html")
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)
@ -611,7 +611,7 @@ class UCPresentationClass(BaseCase):
)
self.begin_presentation(filename="uc_presentation.html")
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)
@ -678,7 +678,7 @@ class UCPresentationClass(BaseCase):
)
self.begin_presentation(filename="uc_presentation.html")
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)
@ -727,7 +727,7 @@ class UCPresentationClass(BaseCase):
)
self.begin_presentation(filename="uc_presentation.html")
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)
@ -769,7 +769,7 @@ class UCPresentationClass(BaseCase):
)
self.begin_presentation(filename="uc_presentation.html")
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)
@ -838,11 +838,11 @@ class UCPresentationClass(BaseCase):
)
self.begin_presentation(filename="uc_presentation.html")
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)
@ -866,7 +866,7 @@ class UCPresentationClass(BaseCase):
)
self.begin_presentation(filename="uc_presentation.html")
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)

View File

@ -1,12 +1,11 @@
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")'
sb.uc_open_with_reconnect(url) # The bot-check is later
sb.type(input_field, "github.com/seleniumbase/SeleniumBase")
sb.reconnect(0.1)
sb.uc_click(submit_button, reconnect_time=3.25)
sb.uc_gui_click_captcha()
sb.wait_for_text_not_visible("Checking", timeout=15)

View File

@ -1,13 +1,13 @@
"""SB Manager using UC Mode & PyAutoGUI for bypassing CAPTCHAs."""
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.cloudflare.com/login"
sb.uc_open_with_reconnect(url, 5.5)
sb.uc_gui_handle_captcha() # PyAutoGUI press Tab and Spacebar
sb.sleep(2.5)
with SB(uc=True, test=True, locale_code="en") as sb:
with SB(uc=True, test=True, locale="en") as sb:
url = "https://www.cloudflare.com/login"
sb.uc_open_with_reconnect(url, 5.5)
sb.uc_gui_click_captcha() # PyAutoGUI click. (Linux needs it)

View File

@ -33,7 +33,7 @@
<li><a href="#sb_sf_22"><strong>22. The driver manager (via context manager)</strong></a></li>
<li><a href="#sb_sf_23"><strong>23. The driver manager (via direct import)</strong></a></li>
<li><a href="#sb_sf_24"><strong>24. CDP driver (async/await API. No Selenium)</strong></a></li>
<li><a href="#sb_sf_25"><strong>25. CDP driver (SB-CDP sync API. No Selenium)</strong></a></li>
<li><a href="#sb_sf_25"><strong>25. CDP driver (SB CDP Sync API. No Selenium)</strong></a></li>
</ul>
</blockquote>
@ -1019,14 +1019,13 @@ This format provides a pure CDP way of using SeleniumBase (without Selenium or a
```python
import asyncio
import time
from seleniumbase.undetected import cdp_driver
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"]')
@ -1043,25 +1042,17 @@ if __name__ == "__main__":
(See <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/raw_async.py">examples/cdp_mode/raw_async.py</a> for the test.)
<a id="sb_sf_25"></a>
<h2><img src="https://seleniumbase.github.io/img/logo3b.png" title="SeleniumBase" width="32" /> 25. CDP driver (SB-CDP sync API. No Selenium)</h2>
<h2><img src="https://seleniumbase.github.io/img/logo3b.png" title="SeleniumBase" width="32" /> 25. CDP driver (SB CDP Sync API. No Selenium)</h2>
This format provides a pure CDP way of using SeleniumBase (without Selenium or a test runner). The expanded SB-CDP sync API is used. Here's an example:
This format provides a pure CDP way of using SeleniumBase (without Selenium or a test runner). The expanded SB CDP Sync API is used. Here's an example:
```python
import asyncio
from seleniumbase.core import sb_cdp
from seleniumbase.undetected import cdp_driver
from seleniumbase import sb_cdp
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")

View File

@ -104,7 +104,7 @@ Sometimes you need to add <code translate="no">incognito=True</code> with <code
```python
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")'

View File

@ -12,14 +12,22 @@ https://docs.docker.com/engine/install/
#### 3. Create your Docker image from your Dockerfile: (Get ready to wait awhile)
**(Windows / Linux / Intel macOS)**
docker build -t seleniumbase .
**(NOTE) - If running on an Apple M1/M2 Mac, use this instead:**
**(Apple Silicon macOS, eg. M1/M2/M3/M4):**
Users should first [Enable Rosetta in Docker Desktop](https://stackoverflow.com/a/76586216/7058266). (Otherwise Chrome will crash on launch with errors such as: `"InvalidSessionIdException"` and `"Unable to receive message from renderer"`)
<img width="420" alt="Enable Rosetta" src="https://seleniumbase.github.io/other/docker_rosetta.jpg" />
Then you can run these commands:
export DOCKER_DEFAULT_PLATFORM=linux/amd64
docker build --platform linux/amd64 -t seleniumbase .
**M1/M2 Mac users** should also see [StackOverflow.com/a/76586216/7058266](https://stackoverflow.com/a/76586216/7058266) to **Enable Rosetta in Docker Desktop**. (Otherwise **you will** encounter errors like this when Chrome tries to launch: `"Chrome failed to start: crashed."`)
#### 4. Run [the example test](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/my_first_test.py) with Chrome inside your Docker: (Once the test completes after a few seconds, you'll automatically exit the Docker shell)
docker run seleniumbase ./run_docker_test_in_chrome.sh

View File

@ -1,7 +1,7 @@
pip>=25.0.1
packaging>=24.2
setuptools~=70.2;python_version<"3.10"
setuptools>=75.8.0;python_version>="3.10"
setuptools>=75.8.2;python_version>="3.10"
wheel>=0.45.1
attrs>=25.1.0
certifi>=2025.1.31
@ -39,7 +39,7 @@ h11==0.14.0
outcome==1.3.0.post0
trio==0.27.0;python_version<"3.9"
trio==0.29.0;python_version>="3.9"
trio-websocket==0.12.1
trio-websocket==0.12.2
wsproto==1.2.0
websocket-client==1.8.0
selenium==4.27.1;python_version<"3.9"

View File

@ -8,6 +8,7 @@ from seleniumbase.__version__ import __version__
from seleniumbase.common import decorators # noqa
from seleniumbase.common import encryption # noqa
from seleniumbase.core import colored_traceback
from seleniumbase.core import sb_cdp # noqa
from seleniumbase.core.browser_launcher import get_driver # noqa
from seleniumbase.fixtures import js_utils # noqa
from seleniumbase.fixtures import page_actions # noqa
@ -18,6 +19,7 @@ from seleniumbase.masterqa.master_qa import MasterQA # noqa
from seleniumbase.plugins.sb_manager import SB # noqa
from seleniumbase.plugins.driver_manager import Driver # noqa
from seleniumbase.plugins.driver_manager import DriverContext # noqa
from seleniumbase.undetected import cdp_driver # noqa
from seleniumbase import translate # noqa
with suppress(Exception):

View File

@ -1,2 +1,2 @@
# seleniumbase package
__version__ = "4.35.1"
__version__ = "4.35.2"

View File

@ -235,8 +235,12 @@ def extend_driver(driver, proxy_auth=False, use_uc=True):
driver.reset_window_size = DM.reset_window_size
if hasattr(driver, "proxy"):
driver.set_wire_proxy = DM.set_wire_proxy
if proxy_auth and not use_uc:
time.sleep(0.11) # Proxy needs moment to load in Manifest V3
if proxy_auth:
# Proxy needs a moment to load in Manifest V3
if use_uc:
time.sleep(0.12)
else:
time.sleep(0.22)
return driver

View File

@ -1,4 +1,5 @@
"""Add CDP methods to extend the driver"""
import asyncio
import fasteners
import os
import re
@ -11,6 +12,7 @@ from seleniumbase.fixtures import constants
from seleniumbase.fixtures import js_utils
from seleniumbase.fixtures import page_utils
from seleniumbase.fixtures import shared_utils
from seleniumbase.undetected.cdp_driver import cdp_util
class CDPMethods():
@ -208,14 +210,16 @@ class CDPMethods():
element = self.__add_sync_methods(element)
return self.__add_sync_methods(element)
elif (
element.parent
element
and element.parent
and tag_name in element.parent.tag_name.lower()
and text.strip() in element.parent.text
):
element = self.__add_sync_methods(element.parent)
return self.__add_sync_methods(element)
elif (
element.parent
element
and element.parent
and element.parent.parent
and tag_name in element.parent.parent.tag_name.lower()
and text.strip() in element.parent.parent.text
@ -269,7 +273,8 @@ class CDPMethods():
if element not in updated_elements:
updated_elements.append(element)
elif (
element.parent
element
and element.parent
and tag_name in element.parent.tag_name.lower()
and text.strip() in element.parent.text
):
@ -277,7 +282,8 @@ class CDPMethods():
if element not in updated_elements:
updated_elements.append(element)
elif (
element.parent
element
and element.parent
and element.parent.parent
and tag_name in element.parent.parent.tag_name.lower()
and text.strip() in element.parent.parent.text
@ -2093,3 +2099,13 @@ class CDPMethods():
)
else:
self.select(selector).save_screenshot(filename)
class Chrome(CDPMethods):
def __init__(self, url=None, **kwargs):
if not url:
url = "about:blank"
loop = asyncio.new_event_loop()
driver = cdp_util.start_sync(**kwargs)
page = loop.run_until_complete(driver.get(url))
super().__init__(loop, page, driver)

View File

@ -1 +1,3 @@
from seleniumbase.undetected.cdp_driver import cdp_util # noqa
from seleniumbase.undetected.cdp_driver.cdp_util import start_async # noqa
from seleniumbase.undetected.cdp_driver.cdp_util import start_sync # noqa

View File

@ -15,6 +15,7 @@ import urllib.parse
import urllib.request
import warnings
from collections import defaultdict
from seleniumbase import config as sb_config
from typing import List, Set, Tuple, Union
import mycdp as cdp
from . import cdp_util as util
@ -287,6 +288,9 @@ class Browser:
filter(lambda item: item.type_ == "page", self.targets)
)
# Use the tab to navigate to new url
if hasattr(sb_config, "_cdp_locale") and sb_config._cdp_locale:
await connection.send(cdp.page.navigate("about:blank"))
await connection.set_locale(sb_config._cdp_locale)
frame_id, loader_id, *_ = await connection.send(
cdp.page.navigate(url)
)

View File

@ -232,9 +232,9 @@ async def start(
browser_args: Optional[List[str]] = None,
xvfb_metrics: Optional[List[str]] = None, # "Width,Height" for Linux
sandbox: Optional[bool] = True,
lang: Optional[str] = None,
host: Optional[str] = None,
port: Optional[int] = None,
lang: Optional[str] = None, # Set the Language Locale Code
host: Optional[str] = None, # Chrome remote-debugging-host
port: Optional[int] = None, # Chrome remote-debugging-port
xvfb: Optional[int] = None, # Use a special virtual display on Linux
headed: Optional[bool] = None, # Override default Xvfb mode on Linux
expert: Optional[bool] = None, # Open up closed Shadow-root elements
@ -320,7 +320,15 @@ async def start(
time.sleep(0.15)
driver = await Browser.create(config)
if proxy and "@" in str(proxy):
time.sleep(0.11)
time.sleep(0.15)
if lang:
sb_config._cdp_locale = lang
elif "locale" in kwargs:
sb_config._cdp_locale = kwargs["locale"]
elif "locale_code" in kwargs:
sb_config._cdp_locale = kwargs["locale_code"]
else:
sb_config._cdp_locale = None
return driver
@ -360,10 +368,7 @@ def start_sync(*args, **kwargs) -> Browser:
):
loop = kwargs["loop"]
else:
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
loop = asyncio.new_event_loop()
headless = False
binary_location = None
if "browser_executable_path" in kwargs:

View File

@ -150,7 +150,7 @@ setup(
'pip>=25.0.1',
'packaging>=24.2',
'setuptools~=70.2;python_version<"3.10"', # Newer ones had issues
'setuptools>=75.8.0;python_version>="3.10"',
'setuptools>=75.8.2;python_version>="3.10"',
'wheel>=0.45.1',
'attrs>=25.1.0',
"certifi>=2025.1.31",
@ -188,7 +188,7 @@ setup(
'outcome==1.3.0.post0',
'trio==0.27.0;python_version<"3.9"',
'trio==0.29.0;python_version>="3.9"',
'trio-websocket==0.12.1',
'trio-websocket==0.12.2',
'wsproto==1.2.0',
'websocket-client==1.8.0',
'selenium==4.27.1;python_version<"3.9"',
@ -283,7 +283,7 @@ setup(
],
# pip install -e .[psutil]
"psutil": [
"psutil==6.0.0",
"psutil==7.0.0",
],
# pip install -e .[pyautogui]
"pyautogui": [