Update CDP Mode

This commit is contained in:
Michael Mintz 2025-02-26 17:30:42 -05:00
parent 5d732a412f
commit 9beb99fa0e
6 changed files with 47 additions and 14 deletions

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

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