Skip to content

Commit b293b22

Browse files
committed
Update and refactor CDP Mode
1 parent 64a2b46 commit b293b22

4 files changed

Lines changed: 52 additions & 23 deletions

File tree

seleniumbase/core/sb_cdp.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -795,15 +795,13 @@ def click_if_visible(self, selector, timeout=0):
795795
if self.is_element_visible(selector):
796796
with suppress(Exception):
797797
self.click(selector, timeout=1)
798+
elif timeout == 0:
799+
return
798800
else:
799801
with suppress(Exception):
800-
element = self.find_element(selector, timeout=timeout)
801-
self.sleep(0.1)
802-
element.scroll_into_view()
803-
self.sleep(0.1)
804-
element.click()
805-
self.__slow_mode_pause_if_set()
806-
self.loop.run_until_complete(self.page.wait())
802+
self.find_element(selector, timeout=timeout)
803+
if self.is_element_visible(selector):
804+
self.click(selector, timeout=1)
807805

808806
def click_visible_elements(self, selector, limit=0):
809807
"""Finds all matching page elements and clicks visible ones in order.
@@ -2068,14 +2066,35 @@ def __cdp_click_incapsula_hcaptcha(self):
20682066
time.sleep(0.05)
20692067
x_offset = 30
20702068
y_offset = 36
2069+
was_clicked = False
20712070
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
20722071
with gui_lock: # Prevent issues with multiple processes
20732072
self.bring_active_window_to_front()
2074-
time.sleep(0.05)
2073+
time.sleep(0.056)
2074+
if "--debug" in sys.argv:
2075+
displayed_selector = "`%s`" % selector
2076+
if '"' not in selector:
2077+
displayed_selector = '"%s"' % selector
2078+
elif "'" not in selector:
2079+
displayed_selector = "'%s'" % selector
2080+
print(
2081+
" <DEBUG> click_with_offset(%s, %s, %s)"
2082+
% (displayed_selector, x_offset, y_offset)
2083+
)
20752084
with suppress(Exception):
20762085
element.click_with_offset(x_offset, y_offset)
2077-
time.sleep(0.2)
2078-
return True
2086+
was_clicked = True
2087+
time.sleep(0.056)
2088+
if was_clicked:
2089+
# Wait a moment for the click to succeed
2090+
time.sleep(0.25)
2091+
self.__slow_mode_pause_if_set()
2092+
self.loop.run_until_complete(self.page.wait())
2093+
if "--debug" in sys.argv:
2094+
print(" <DEBUG> hCaptcha was clicked!")
2095+
return True
2096+
if "--debug" in sys.argv:
2097+
print(" <DEBUG> hCaptcha was NOT clicked!")
20792098
return False
20802099

20812100
def solve_captcha(self):

seleniumbase/fixtures/base_case.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def test_anything(self):
9292

9393
logging.getLogger("requests").setLevel(logging.ERROR)
9494
logging.getLogger("urllib3").setLevel(logging.ERROR)
95+
logging.getLogger("websocket").setLevel(logging.CRITICAL)
9596
urllib3.disable_warnings()
9697
LOGGER.setLevel(logging.WARNING)
9798
is_linux = shared_utils.is_linux()

seleniumbase/undetected/cdp_driver/browser.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ def stopped(self):
182182
if self._process and self._process.returncode is None:
183183
return False
184184
return True
185-
# return (self._process and self._process.returncode) or False
186185

187186
async def wait(self, time: Union[float, int] = 1) -> Browser:
188187
"""Wait for <time> seconds. Important to use,
@@ -436,6 +435,7 @@ async def get(
436435
"*.adthrive.com*",
437436
"*.pubmatic.com*",
438437
"*.id5-sync.com*",
438+
"*.moatads.com*",
439439
"*.dotomi.com*",
440440
"*.adsrvr.org*",
441441
"*.atmtd.com*",
@@ -572,7 +572,7 @@ async def start(self=None) -> Browser:
572572
If you are sure about the browser executable,
573573
set it using `browser_executable_path='{}` parameter."""
574574
).format(
575-
"/path/to/browser/executable"
575+
"/path/to/your/browser/executable"
576576
if is_posix
577577
else "c:/path/to/your/browser.exe"
578578
)
@@ -592,8 +592,6 @@ async def start(self=None) -> Browser:
592592
if not connect_existing:
593593
self._process: asyncio.subprocess.Process = (
594594
await asyncio.create_subprocess_exec(
595-
# self.config.browser_executable_path,
596-
# *cmdparams,
597595
exe,
598596
*params,
599597
stdin=asyncio.subprocess.PIPE,
@@ -602,19 +600,23 @@ async def start(self=None) -> Browser:
602600
close_fds=is_posix,
603601
)
604602
)
603+
await asyncio.sleep(0.05)
605604
self._process_pid = self._process.pid
605+
await asyncio.sleep(0.05)
606606
self._http = HTTPApi((self.config.host, self.config.port))
607+
await asyncio.sleep(0.05)
607608
get_registered_instances().add(self)
608-
await asyncio.sleep(0.25)
609-
for _ in range(5):
609+
await asyncio.sleep(0.15)
610+
for attempt in range(5):
610611
try:
611612
self.info = ContraDict(
612613
await self._http.get("version"), silent=True
613614
)
614615
except (Exception,):
615-
if _ == 4:
616+
if attempt == 4:
616617
logger.debug("Could not start", exc_info=True)
617-
await self.sleep(0.5)
618+
else:
619+
await self.sleep(0.5)
618620
else:
619621
break
620622
if not self.info:

seleniumbase/undetected/cdp_driver/connection.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88
import sys
99
import types
10+
import warnings
1011
from typing import (
1112
Optional,
1213
Generator,
@@ -445,11 +446,17 @@ async def send(
445446
if not _is_update:
446447
await self._register_handlers()
447448
await self.websocket.send(tx.message)
448-
try:
449-
return await tx
450-
except ProtocolException as e:
451-
e.message += f"\ncommand:{tx.method}\nparams:{tx.params}"
452-
raise e
449+
with warnings.catch_warnings():
450+
warnings.filterwarnings(
451+
"ignore",
452+
message="coroutine .* was never awaited",
453+
category=RuntimeWarning,
454+
)
455+
try:
456+
return await tx
457+
except ProtocolException as e:
458+
e.message += f"\ncommand:{tx.method}\nparams:{tx.params}"
459+
raise e
453460
except Exception:
454461
await self.aclose()
455462

0 commit comments

Comments
 (0)