Skip to content

Commit 9beb99f

Browse files
committed
Update CDP Mode
1 parent 5d732a4 commit 9beb99f

File tree

6 files changed

+47
-14
lines changed

6 files changed

+47
-14
lines changed

seleniumbase/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from seleniumbase.common import decorators # noqa
99
from seleniumbase.common import encryption # noqa
1010
from seleniumbase.core import colored_traceback
11+
from seleniumbase.core import sb_cdp # noqa
1112
from seleniumbase.core.browser_launcher import get_driver # noqa
1213
from seleniumbase.fixtures import js_utils # noqa
1314
from seleniumbase.fixtures import page_actions # noqa
@@ -18,6 +19,7 @@
1819
from seleniumbase.plugins.sb_manager import SB # noqa
1920
from seleniumbase.plugins.driver_manager import Driver # noqa
2021
from seleniumbase.plugins.driver_manager import DriverContext # noqa
22+
from seleniumbase.undetected import cdp_driver # noqa
2123
from seleniumbase import translate # noqa
2224

2325
with suppress(Exception):

seleniumbase/core/browser_launcher.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,12 @@ def extend_driver(driver, proxy_auth=False, use_uc=True):
235235
driver.reset_window_size = DM.reset_window_size
236236
if hasattr(driver, "proxy"):
237237
driver.set_wire_proxy = DM.set_wire_proxy
238-
if proxy_auth and not use_uc:
239-
time.sleep(0.11) # Proxy needs moment to load in Manifest V3
238+
if proxy_auth:
239+
# Proxy needs a moment to load in Manifest V3
240+
if use_uc:
241+
time.sleep(0.12)
242+
else:
243+
time.sleep(0.22)
240244
return driver
241245

242246

seleniumbase/core/sb_cdp.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Add CDP methods to extend the driver"""
2+
import asyncio
23
import fasteners
34
import os
45
import re
@@ -11,6 +12,7 @@
1112
from seleniumbase.fixtures import js_utils
1213
from seleniumbase.fixtures import page_utils
1314
from seleniumbase.fixtures import shared_utils
15+
from seleniumbase.undetected.cdp_driver import cdp_util
1416

1517

1618
class CDPMethods():
@@ -208,14 +210,16 @@ def find_element_by_text(self, text, tag_name=None, timeout=None):
208210
element = self.__add_sync_methods(element)
209211
return self.__add_sync_methods(element)
210212
elif (
211-
element.parent
213+
element
214+
and element.parent
212215
and tag_name in element.parent.tag_name.lower()
213216
and text.strip() in element.parent.text
214217
):
215218
element = self.__add_sync_methods(element.parent)
216219
return self.__add_sync_methods(element)
217220
elif (
218-
element.parent
221+
element
222+
and element.parent
219223
and element.parent.parent
220224
and tag_name in element.parent.parent.tag_name.lower()
221225
and text.strip() in element.parent.parent.text
@@ -269,15 +273,17 @@ def find_elements_by_text(self, text, tag_name=None):
269273
if element not in updated_elements:
270274
updated_elements.append(element)
271275
elif (
272-
element.parent
276+
element
277+
and element.parent
273278
and tag_name in element.parent.tag_name.lower()
274279
and text.strip() in element.parent.text
275280
):
276281
element = self.__add_sync_methods(element.parent)
277282
if element not in updated_elements:
278283
updated_elements.append(element)
279284
elif (
280-
element.parent
285+
element
286+
and element.parent
281287
and element.parent.parent
282288
and tag_name in element.parent.parent.tag_name.lower()
283289
and text.strip() in element.parent.parent.text
@@ -2093,3 +2099,13 @@ def save_screenshot(self, name, folder=None, selector=None):
20932099
)
20942100
else:
20952101
self.select(selector).save_screenshot(filename)
2102+
2103+
2104+
class Chrome(CDPMethods):
2105+
def __init__(self, url=None, **kwargs):
2106+
if not url:
2107+
url = "about:blank"
2108+
loop = asyncio.new_event_loop()
2109+
driver = cdp_util.start_sync(**kwargs)
2110+
page = loop.run_until_complete(driver.get(url))
2111+
super().__init__(loop, page, driver)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
from seleniumbase.undetected.cdp_driver import cdp_util # noqa
2+
from seleniumbase.undetected.cdp_driver.cdp_util import start_async # noqa
3+
from seleniumbase.undetected.cdp_driver.cdp_util import start_sync # noqa

seleniumbase/undetected/cdp_driver/browser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import urllib.request
1616
import warnings
1717
from collections import defaultdict
18+
from seleniumbase import config as sb_config
1819
from typing import List, Set, Tuple, Union
1920
import mycdp as cdp
2021
from . import cdp_util as util
@@ -287,6 +288,9 @@ async def get(
287288
filter(lambda item: item.type_ == "page", self.targets)
288289
)
289290
# Use the tab to navigate to new url
291+
if hasattr(sb_config, "_cdp_locale") and sb_config._cdp_locale:
292+
await connection.send(cdp.page.navigate("about:blank"))
293+
await connection.set_locale(sb_config._cdp_locale)
290294
frame_id, loader_id, *_ = await connection.send(
291295
cdp.page.navigate(url)
292296
)

seleniumbase/undetected/cdp_driver/cdp_util.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,9 @@ async def start(
232232
browser_args: Optional[List[str]] = None,
233233
xvfb_metrics: Optional[List[str]] = None, # "Width,Height" for Linux
234234
sandbox: Optional[bool] = True,
235-
lang: Optional[str] = None,
236-
host: Optional[str] = None,
237-
port: Optional[int] = None,
235+
lang: Optional[str] = None, # Set the Language Locale Code
236+
host: Optional[str] = None, # Chrome remote-debugging-host
237+
port: Optional[int] = None, # Chrome remote-debugging-port
238238
xvfb: Optional[int] = None, # Use a special virtual display on Linux
239239
headed: Optional[bool] = None, # Override default Xvfb mode on Linux
240240
expert: Optional[bool] = None, # Open up closed Shadow-root elements
@@ -320,7 +320,15 @@ async def start(
320320
time.sleep(0.15)
321321
driver = await Browser.create(config)
322322
if proxy and "@" in str(proxy):
323-
time.sleep(0.11)
323+
time.sleep(0.15)
324+
if lang:
325+
sb_config._cdp_locale = lang
326+
elif "locale" in kwargs:
327+
sb_config._cdp_locale = kwargs["locale"]
328+
elif "locale_code" in kwargs:
329+
sb_config._cdp_locale = kwargs["locale_code"]
330+
else:
331+
sb_config._cdp_locale = None
324332
return driver
325333

326334

@@ -360,10 +368,7 @@ def start_sync(*args, **kwargs) -> Browser:
360368
):
361369
loop = kwargs["loop"]
362370
else:
363-
try:
364-
loop = asyncio.get_event_loop()
365-
except RuntimeError:
366-
loop = asyncio.new_event_loop()
371+
loop = asyncio.new_event_loop()
367372
headless = False
368373
binary_location = None
369374
if "browser_executable_path" in kwargs:

0 commit comments

Comments
 (0)