diff --git a/examples/cdp_mode/raw_async.py b/examples/cdp_mode/raw_async.py index 6835286498e..74b85a078ab 100644 --- a/examples/cdp_mode/raw_async.py +++ b/examples/cdp_mode/raw_async.py @@ -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) diff --git a/examples/cdp_mode/raw_cdp.py b/examples/cdp_mode/raw_cdp.py index 54d6b28c01d..990f10fa43e 100644 --- a/examples/cdp_mode/raw_cdp.py +++ b/examples/cdp_mode/raw_cdp.py @@ -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) diff --git a/examples/cdp_mode/raw_cdp_with_sb.py b/examples/cdp_mode/raw_cdp_with_sb.py index 6b1d74a1f59..6821bbdd1a0 100644 --- a/examples/cdp_mode/raw_cdp_with_sb.py +++ b/examples/cdp_mode/raw_cdp_with_sb.py @@ -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) diff --git a/seleniumbase/__version__.py b/seleniumbase/__version__.py index bec1f2f6776..059aca1510b 100755 --- a/seleniumbase/__version__.py +++ b/seleniumbase/__version__.py @@ -1,2 +1,2 @@ # seleniumbase package -__version__ = "4.32.4" +__version__ = "4.32.5" diff --git a/seleniumbase/core/browser_launcher.py b/seleniumbase/core/browser_launcher.py index 74b2aebe1de..8acd0499e0d 100644 --- a/seleniumbase/core/browser_launcher.py +++ b/seleniumbase/core/browser_launcher.py @@ -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: @@ -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): diff --git a/seleniumbase/fixtures/base_case.py b/seleniumbase/fixtures/base_case.py index c439a7e8891..c1f3fb801fc 100644 --- a/seleniumbase/fixtures/base_case.py +++ b/seleniumbase/fixtures/base_case.py @@ -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, diff --git a/seleniumbase/plugins/driver_manager.py b/seleniumbase/plugins/driver_manager.py index 00de9de3d05..b47bc1acbfc 100644 --- a/seleniumbase/plugins/driver_manager.py +++ b/seleniumbase/plugins/driver_manager.py @@ -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 @@ -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, diff --git a/seleniumbase/undetected/__init__.py b/seleniumbase/undetected/__init__.py index cbd04f0ec9f..24895b78ffd 100644 --- a/seleniumbase/undetected/__init__.py +++ b/seleniumbase/undetected/__init__.py @@ -235,6 +235,7 @@ def __init__( "--no-first-run", "--no-service-autorun", "--password-store=basic", + "--profile-directory=Default", ] ) options.add_argument( @@ -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: @@ -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 @@ -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): @@ -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 @@ -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") diff --git a/seleniumbase/undetected/cdp_driver/browser.py b/seleniumbase/undetected/cdp_driver/browser.py index 7571cb87d2b..882de93edca 100644 --- a/seleniumbase/undetected/cdp_driver/browser.py +++ b/seleniumbase/undetected/cdp_driver/browser.py @@ -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: