Skip to content

Commit

Permalink
Merge pull request #3235 from seleniumbase/cdp-mode-patch-5
Browse files Browse the repository at this point in the history
CDP Mode - Patch 5
  • Loading branch information
mdmintz authored Oct 29, 2024
2 parents 533619d + 7d50bb6 commit d0625e8
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 183 deletions.
5 changes: 4 additions & 1 deletion examples/cdp_mode/raw_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ async def main():
sb.sleep(2.5)
sb.remove_elements("msm-cookie-banner")
sb.sleep(1.5)
sb.press_keys('input[data-test-id*="search"]', "Amsterdam")
location = "Amsterdam"
sb.press_keys('input[data-test-id*="search"]', location)
sb.sleep(1)
sb.click('input[data-test-id*="search"]')
sb.sleep(2)
sb.click('span[data-test-id*="autocomplete"]')
sb.sleep(5)
Expand Down
2 changes: 2 additions & 0 deletions examples/cdp_mode/raw_cdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def main():
sb.sleep(1.5)
location = "Amsterdam"
sb.press_keys('input[data-test-id*="search"]', location)
sb.sleep(1)
sb.click('input[data-test-id*="search"]')
sb.sleep(2)
sb.click('span[data-test-id*="autocomplete"]')
sb.sleep(5)
Expand Down
2 changes: 2 additions & 0 deletions examples/cdp_mode/raw_cdp_with_sb.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
sb.sleep(1.5)
location = "Amsterdam"
sb.press_keys('input[data-test-id*="search"]', location)
sb.sleep(1)
sb.click('input[data-test-id*="search"]')
sb.sleep(2)
sb.click('span[data-test-id*="autocomplete"]')
sb.sleep(5)
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.4"
__version__ = "4.32.5"
40 changes: 38 additions & 2 deletions seleniumbase/core/browser_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,33 @@ def uc_open_with_cdp_mode(driver, url=None):
driver.cdp_base = loop.run_until_complete(
cdp_util.start(host=cdp_host, port=cdp_port)
)
loop.run_until_complete(driver.cdp_base.wait(0))

if (
"chrome-extension://" in str(driver.cdp_base.main_tab)
and len(driver.cdp_base.tabs) >= 2
):
with suppress(Exception):
loop.run_until_complete(driver.cdp_base.main_tab.close())

for tab in driver.cdp_base.tabs[-1::-1]:
if "chrome-extension://" not in str(tab):
loop.run_until_complete(tab.activate())
break

page_tab = None
if "chrome-extension://" not in str(driver.cdp_base.tabs[-1]):
page_tab = driver.cdp_base.tabs[-1]
else:
for tab in driver.cdp_base.tabs:
if "chrome-extension://" not in str(tab):
page_tab = tab
break
if page_tab:
loop.run_until_complete(page_tab.aopen())
loop.run_until_complete(page_tab.activate())

loop.run_until_complete(driver.cdp_base.update_targets())
page = loop.run_until_complete(driver.cdp_base.get(url))
loop.run_until_complete(page.activate())
if not safe_url:
Expand Down Expand Up @@ -1106,13 +1132,23 @@ def _uc_gui_click_captcha(
frame = '[data-callback="onCaptchaSuccess"]'
else:
return
if driver.is_element_present('form[class*=center]'):
if (
driver.is_element_present("form")
and (
driver.is_element_present('form[class*="center"]')
or driver.is_element_present('form[class*="right"]')
or driver.is_element_present('form div[class*="center"]')
or driver.is_element_present('form div[class*="right"]')
)
):
script = (
"""var $elements = document.querySelectorAll('form');
"""var $elements = document.querySelectorAll(
'form[class], form div[class]');
var index = 0, length = $elements.length;
for(; index < length; index++){
the_class = $elements[index].getAttribute('class');
new_class = the_class.replaceAll('center', 'left');
new_class = new_class.replaceAll('right', 'left');
$elements[index].setAttribute('class', new_class);}"""
)
if __is_cdp_swap_needed(driver):
Expand Down
84 changes: 0 additions & 84 deletions seleniumbase/fixtures/base_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -4090,90 +4090,6 @@ def get_new_driver(
"Browser: {%s} is not a valid browser option. "
"Valid options = {%s}" % (browser, valid_browsers)
)
# Fix Chrome-130 issues by creating a user-data-dir in advance
if (
undetectable
and (
not user_data_dir
or not os.path.exists(user_data_dir)
or not any(os.scandir(user_data_dir))
)
and self.browser == "chrome"
and shared_utils.is_chrome_130_or_newer(binary_location)
):
import tempfile
if not user_data_dir:
user_data_dir = os.path.normpath(tempfile.mkdtemp())
self.user_data_dir = user_data_dir
sb_config.user_data_dir = user_data_dir
try:
decoy_driver = browser_launcher.get_driver(
browser_name=browser_name,
headless=False,
locale_code=locale_code,
use_grid=use_grid,
protocol=protocol,
servername=servername,
port=port,
proxy_string=proxy_string,
proxy_bypass_list=proxy_bypass_list,
proxy_pac_url=proxy_pac_url,
multi_proxy=multi_proxy,
user_agent=user_agent,
cap_file=cap_file,
cap_string=cap_string,
recorder_ext=recorder_ext,
disable_cookies=disable_cookies,
disable_js=disable_js,
disable_csp=disable_csp,
enable_ws=enable_ws,
enable_sync=enable_sync,
use_auto_ext=use_auto_ext,
undetectable=undetectable,
uc_cdp_events=uc_cdp_events,
uc_subprocess=uc_subprocess,
log_cdp_events=log_cdp_events,
no_sandbox=no_sandbox,
disable_gpu=disable_gpu,
headless1=False,
headless2=True,
incognito=incognito,
guest_mode=guest_mode,
dark_mode=dark_mode,
devtools=devtools,
remote_debug=remote_debug,
enable_3d_apis=enable_3d_apis,
swiftshader=swiftshader,
ad_block_on=ad_block_on,
host_resolver_rules=host_resolver_rules,
block_images=block_images,
do_not_track=do_not_track,
chromium_arg="decoy",
firefox_arg=firefox_arg,
firefox_pref=firefox_pref,
user_data_dir=user_data_dir,
extension_zip=None,
extension_dir=None,
disable_features=disable_features,
binary_location=binary_location,
driver_version=driver_version,
page_load_strategy=page_load_strategy,
use_wire=use_wire,
external_pdf=external_pdf,
test_id=test_id,
mobile_emulator=is_mobile,
device_width=d_width,
device_height=d_height,
device_pixel_ratio=d_p_r,
browser=browser_name,
)
time.sleep(0.16)
except Exception:
pass
finally:
with suppress(Exception):
decoy_driver.quit()
time.sleep(0.08)
# Launch a web browser
new_driver = browser_launcher.get_driver(
browser_name=browser_name,
Expand Down
90 changes: 1 addition & 89 deletions seleniumbase/plugins/driver_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ def Driver(
wire (bool): Shortcut / Duplicate of "use_wire".
pls (str): Shortcut / Duplicate of "page_load_strategy".
"""
from contextlib import suppress
from seleniumbase import config as sb_config
from seleniumbase.config import settings
from seleniumbase.core import browser_launcher
from seleniumbase.fixtures import constants
from seleniumbase.fixtures import shared_utils

Expand Down Expand Up @@ -790,94 +790,6 @@ def Driver(
browser_name = browser

# Launch a web browser
from seleniumbase.core import browser_launcher

# Fix Chrome-130 issues by creating a user-data-dir in advance
if (
undetectable
and (
not user_data_dir
or not os.path.exists(user_data_dir)
or not any(os.scandir(user_data_dir))
)
and browser == "chrome"
and shared_utils.is_chrome_130_or_newer(binary_location)
):
import tempfile
import time
if not user_data_dir:
user_data_dir = (
os.path.normpath(tempfile.mkdtemp())
)
try:
decoy_driver = browser_launcher.get_driver(
browser_name=browser_name,
headless=False,
locale_code=locale_code,
use_grid=use_grid,
protocol=protocol,
servername=servername,
port=port,
proxy_string=proxy_string,
proxy_bypass_list=proxy_bypass_list,
proxy_pac_url=proxy_pac_url,
multi_proxy=multi_proxy,
user_agent=user_agent,
cap_file=cap_file,
cap_string=cap_string,
recorder_ext=recorder_ext,
disable_cookies=disable_cookies,
disable_js=disable_js,
disable_csp=disable_csp,
enable_ws=enable_ws,
enable_sync=enable_sync,
use_auto_ext=use_auto_ext,
undetectable=undetectable,
uc_cdp_events=uc_cdp_events,
uc_subprocess=uc_subprocess,
log_cdp_events=log_cdp_events,
no_sandbox=no_sandbox,
disable_gpu=disable_gpu,
headless1=False,
headless2=True,
incognito=incognito,
guest_mode=guest_mode,
dark_mode=dark_mode,
devtools=devtools,
remote_debug=remote_debug,
enable_3d_apis=enable_3d_apis,
swiftshader=swiftshader,
ad_block_on=ad_block_on,
host_resolver_rules=host_resolver_rules,
block_images=block_images,
do_not_track=do_not_track,
chromium_arg="decoy",
firefox_arg=firefox_arg,
firefox_pref=firefox_pref,
user_data_dir=user_data_dir,
extension_zip=None,
extension_dir=None,
disable_features=disable_features,
binary_location=binary_location,
driver_version=driver_version,
page_load_strategy=page_load_strategy,
use_wire=use_wire,
external_pdf=external_pdf,
test_id=test_id,
mobile_emulator=is_mobile,
device_width=d_width,
device_height=d_height,
device_pixel_ratio=d_p_r,
browser=browser_name,
)
time.sleep(0.16)
except Exception:
pass
finally:
with suppress(Exception):
decoy_driver.quit()
time.sleep(0.08)

driver = browser_launcher.get_driver(
browser_name=browser_name,
headless=headless,
Expand Down
21 changes: 16 additions & 5 deletions seleniumbase/undetected/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ def __init__(
"--no-first-run",
"--no-service-autorun",
"--password-store=basic",
"--profile-directory=Default",
]
)
options.add_argument(
Expand Down Expand Up @@ -423,7 +424,9 @@ def reconnect(self, timeout=0.1):
- Recreates the session."""
if hasattr(self, "service"):
with suppress(Exception):
self.service.stop()
if self.service.is_connectable():
self.stop_client()
self.service.stop()
if isinstance(timeout, str):
if timeout.lower() == "breakpoint":
breakpoint() # To continue:
Expand All @@ -437,7 +440,9 @@ def reconnect(self, timeout=0.1):
with suppress(Exception):
if self.current_url.startswith("chrome-extension://"):
self.close()
self.service.stop()
if self.service.is_connectable():
self.stop_client()
self.service.stop()
self.service.start()
self.start_session()
self._is_connected = True
Expand All @@ -447,7 +452,9 @@ def disconnect(self):
To use driver methods again, you MUST call driver.connect()"""
if hasattr(self, "service"):
with suppress(Exception):
self.service.stop()
if self.service.is_connectable():
self.stop_client()
self.service.stop()
self._is_connected = False

def connect(self):
Expand All @@ -461,7 +468,9 @@ def connect(self):
with suppress(Exception):
if self.current_url.startswith("chrome-extension://"):
self.close()
self.service.stop()
if self.service.is_connectable():
self.stop_client()
self.service.stop()
self.service.start()
self.start_session()
self._is_connected = True
Expand All @@ -488,7 +497,9 @@ def quit(self):
pass
if hasattr(self, "service") and getattr(self.service, "process", None):
logger.debug("Stopping webdriver service")
self.service.stop()
with suppress(Exception):
self.stop_client()
self.service.stop()
with suppress(Exception):
if self.reactor and isinstance(self.reactor, Reactor):
logger.debug("Shutting down Reactor")
Expand Down
2 changes: 1 addition & 1 deletion seleniumbase/undetected/cdp_driver/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def _handle_target_update(

async def get(
self,
url="chrome://welcome",
url="about:blank",
new_tab: bool = False,
new_window: bool = False,
) -> tab.Tab:
Expand Down

0 comments on commit d0625e8

Please sign in to comment.