Skip to content

CDP Mode - Patch 9 #3252

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 4 commits into from
Nov 9, 2024
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
3 changes: 3 additions & 0 deletions examples/cdp_mode/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ sb.cdp.get_element_attribute(selector, attribute)
sb.cdp.get_element_html(selector)
sb.cdp.set_locale(locale)
sb.cdp.set_attributes(selector, attribute, value)
sb.cdp.gui_press_key(key)
sb.cdp.gui_press_keys(keys)
sb.cdp.gui_write(text)
sb.cdp.gui_click_x_y(x, y)
sb.cdp.gui_click_element(selector)
sb.cdp.internalize_links()
Expand Down
35 changes: 35 additions & 0 deletions examples/cdp_mode/raw_albertsons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en") as sb:
url = "https://www.albertsons.com/recipes/"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
sb.remove_element("div > div > article")
sb.cdp.scroll_into_view('input[type="search"]')
sb.cdp.click_if_visible("button.banner-close-button")
sb.cdp.click("input#search-suggestion-input")
sb.sleep(0.2)
search = "Avocado Smoked Salmon"
required_text = "Salmon"
sb.cdp.press_keys("input#search-suggestion-input", search)
sb.sleep(0.8)
sb.cdp.click("#suggestion-0 a span")
sb.sleep(3.2)
sb.cdp.click_if_visible("button.banner-close-button")
sb.sleep(1.2)
print('*** Albertsons Search for "%s":' % search)
print(' (Results must contain "%s".)' % required_text)
unique_item_text = []
item_selector = 'a[href*="/meal-plans-recipes/shop/"]'
info_selector = 'span[data-test-id*="recipe-thumb-title"]'
items = sb.cdp.find_elements("%s %s" % (item_selector, info_selector))
for item in items:
sb.sleep(0.03)
item.scroll_into_view()
sb.sleep(0.025)
if required_text in item.text:
item.flash()
sb.sleep(0.025)
if item.text not in unique_item_text:
unique_item_text.append(item.text)
print("* " + item.text)
7 changes: 4 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
pip>=24.2
packaging>=24.1
packaging>=24.2
setuptools~=70.2;python_version<"3.10"
setuptools>=73.0.1;python_version>="3.10"
wheel>=0.44.0
wheel>=0.45.0
attrs>=24.2.0
certifi>=2024.8.30
exceptiongroup>=1.2.2
websockets>=13.1
websockets~=13.1;python_version<"3.9"
websockets>=14.0;python_version>="3.9"
filelock>=3.16.1
fasteners>=0.19
mycdp>=1.0.1
Expand Down
2 changes: 1 addition & 1 deletion seleniumbase/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# seleniumbase package
__version__ = "4.32.8"
__version__ = "4.32.9"
6 changes: 6 additions & 0 deletions seleniumbase/core/browser_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,9 @@ def uc_open_with_cdp_mode(driver, url=None):
cdp.reset_window_size = CDPM.reset_window_size
cdp.set_locale = CDPM.set_locale
cdp.set_attributes = CDPM.set_attributes
cdp.gui_press_key = CDPM.gui_press_key
cdp.gui_press_keys = CDPM.gui_press_keys
cdp.gui_write = CDPM.gui_write
cdp.gui_click_x_y = CDPM.gui_click_x_y
cdp.gui_click_element = CDPM.gui_click_element
cdp.internalize_links = CDPM.internalize_links
Expand Down Expand Up @@ -721,6 +724,9 @@ def uc_click(
timeout=settings.SMALL_TIMEOUT,
reconnect_time=None,
):
if __is_cdp_swap_needed(driver):
driver.cdp.click(selector)
return
with suppress(Exception):
rct = float(by) # Add shortcut: driver.uc_click(selector, RCT)
if not reconnect_time:
Expand Down
39 changes: 39 additions & 0 deletions seleniumbase/core/sb_cdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,45 @@ def __get_configured_pyautogui(self, pyautogui_copy):
)
return pyautogui_copy

def gui_press_key(self, key):
self.__install_pyautogui_if_missing()
import pyautogui
pyautogui = self.__get_configured_pyautogui(pyautogui)
gui_lock = fasteners.InterProcessLock(
constants.MultiBrowser.PYAUTOGUILOCK
)
with gui_lock:
pyautogui.press(key)
time.sleep(0.0375)
self.__slow_mode_pause_if_set()
self.loop.run_until_complete(self.page.wait())

def gui_press_keys(self, keys):
self.__install_pyautogui_if_missing()
import pyautogui
pyautogui = self.__get_configured_pyautogui(pyautogui)
gui_lock = fasteners.InterProcessLock(
constants.MultiBrowser.PYAUTOGUILOCK
)
with gui_lock:
for key in keys:
pyautogui.press(key)
time.sleep(0.0375)
self.__slow_mode_pause_if_set()
self.loop.run_until_complete(self.page.wait())

def gui_write(self, text):
self.__install_pyautogui_if_missing()
import pyautogui
pyautogui = self.__get_configured_pyautogui(pyautogui)
gui_lock = fasteners.InterProcessLock(
constants.MultiBrowser.PYAUTOGUILOCK
)
with gui_lock:
pyautogui.write(text)
self.__slow_mode_pause_if_set()
self.loop.run_until_complete(self.page.wait())

def __gui_click_x_y(self, x, y, timeframe=0.25, uc_lock=False):
self.__install_pyautogui_if_missing()
import pyautogui
Expand Down
6 changes: 1 addition & 5 deletions seleniumbase/plugins/driver_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,11 +550,7 @@ def Driver(
or uc_sub
):
undetectable = True
if (
(undetectable or undetected or uc)
and (uc_subprocess is None)
and (uc_sub is None)
):
if undetectable or undetected or uc:
uc_subprocess = True # Use UC as a subprocess by default.
elif (
"--undetectable" in sys_argv
Expand Down
6 changes: 1 addition & 5 deletions seleniumbase/plugins/sb_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,11 +608,7 @@ def SB(
or uc_sub
):
undetectable = True
if (
(undetectable or undetected or uc)
and (uc_subprocess is None)
and (uc_sub is None)
):
if undetectable or undetected or uc:
uc_subprocess = True # Use UC as a subprocess by default.
elif (
"--undetectable" in sys_argv
Expand Down
7 changes: 4 additions & 3 deletions seleniumbase/undetected/cdp_driver/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
TypeVar,
)
import websockets
from websockets.protocol import State
from . import cdp_util as util
import mycdp as cdp
import mycdp.network
Expand Down Expand Up @@ -261,7 +262,7 @@ async def aopen(self, **kw):
"""
Opens the websocket connection. Shouldn't be called manually by users.
"""
if not self.websocket or self.websocket.closed:
if not self.websocket or self.websocket.state is State.CLOSED:
try:
self.websocket = await websockets.connect(
self.websocket_url,
Expand All @@ -288,7 +289,7 @@ async def aclose(self):
"""
Closes the websocket connection. Shouldn't be called manually by users.
"""
if self.websocket and not self.websocket.closed:
if self.websocket and self.websocket.state is not State.CLOSED:
if self.listener and self.listener.running:
self.listener.cancel()
self.enabled_domains.clear()
Expand Down Expand Up @@ -393,7 +394,7 @@ async def send(
when multiple calls to connection.send() are made.
"""
await self.aopen()
if not self.websocket or self.closed:
if not self.websocket or self.websocket.state is State.CLOSED:
return
if self._owner:
browser = self._owner
Expand Down
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,15 @@
python_requires=">=3.8",
install_requires=[
'pip>=24.2',
'packaging>=24.1',
'packaging>=24.2',
'setuptools~=70.2;python_version<"3.10"', # Newer ones had issues
'setuptools>=73.0.1;python_version>="3.10"',
'wheel>=0.44.0',
'wheel>=0.45.0',
'attrs>=24.2.0',
"certifi>=2024.8.30",
"exceptiongroup>=1.2.2",
"websockets>=13.1",
'websockets~=13.1;python_version<"3.9"',
'websockets>=14.0;python_version>="3.9"',
'filelock>=3.16.1',
'fasteners>=0.19',
"mycdp>=1.0.1",
Expand Down