Skip to content

Commit 04719e5

Browse files
committedJan 9, 2025
Update CDP Mode
1 parent 11e81b0 commit 04719e5

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed
 

‎seleniumbase/core/browser_launcher.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,26 @@ def uc_open_with_cdp_mode(driver, url=None):
533533
if url_protocol not in ["about", "data", "chrome"]:
534534
safe_url = False
535535

536+
headless = False
537+
headed = None
538+
xvfb = None
539+
if hasattr(sb_config, "headless"):
540+
headless = sb_config.headless
541+
if hasattr(sb_config, "headed"):
542+
headed = sb_config.headed
543+
if hasattr(sb_config, "xvfb"):
544+
xvfb = sb_config.xvfb
545+
536546
loop = asyncio.new_event_loop()
537547
asyncio.set_event_loop(loop)
538548
driver.cdp_base = loop.run_until_complete(
539-
cdp_util.start(host=cdp_host, port=cdp_port)
549+
cdp_util.start(
550+
host=cdp_host,
551+
port=cdp_port,
552+
headless=headless,
553+
headed=headed,
554+
xvfb=xvfb,
555+
)
540556
)
541557
loop.run_until_complete(driver.cdp_base.wait(0))
542558

@@ -863,13 +879,15 @@ def __install_pyautogui_if_missing():
863879
xvfb_height = 768
864880
sb_config._xvfb_height = xvfb_height
865881
with suppress(Exception):
866-
xvfb_display = Display(
882+
_xvfb_display = Display(
867883
visible=True,
868884
size=(xvfb_width, xvfb_height),
869885
backend="xvfb",
870886
use_xauth=True,
871887
)
872-
xvfb_display.start()
888+
_xvfb_display.start()
889+
sb_config._virtual_display = _xvfb_display
890+
sb_config.headless_active = True
873891

874892

875893
def install_pyautogui_if_missing(driver):

‎seleniumbase/fixtures/base_case.py

+22-3
Original file line numberDiff line numberDiff line change
@@ -13795,7 +13795,8 @@ def __switch_to_newest_window_if_not_blank(self):
1379513795
if self.get_current_url() == "about:blank":
1379613796
self.switch_to_window(current_window)
1379713797
except Exception:
13798-
self.switch_to_window(current_window)
13798+
with suppress(Exception):
13799+
self.switch_to_window(current_window)
1379913800

1380013801
def __needs_minimum_wait(self):
1380113802
if (
@@ -14004,9 +14005,10 @@ def __activate_standard_virtual_display(self):
1400414005
visible=0, size=(width, height)
1400514006
)
1400614007
self._xvfb_display.start()
14007-
sb_config._virtual_display = self._xvfb_display
1400814008
self.headless_active = True
14009-
sb_config.headless_active = True
14009+
if not self.undetectable:
14010+
sb_config._virtual_display = self._xvfb_display
14011+
sb_config.headless_active = True
1401014012

1401114013
def __activate_virtual_display(self):
1401214014
if self.undetectable and not (self.headless or self.headless2):
@@ -14029,6 +14031,8 @@ def __activate_virtual_display(self):
1402914031
"\nX11 display failed! Will use regular xvfb!"
1403014032
)
1403114033
self.__activate_standard_virtual_display()
14034+
else:
14035+
self.headless_active = True
1403214036
except Exception as e:
1403314037
if hasattr(e, "msg"):
1403414038
print("\n" + str(e.msg))
@@ -16601,6 +16605,7 @@ def tearDown(self):
1660116605
self.__quit_all_drivers()
1660216606
# Resume tearDown() for all test runners, (Pytest / Pynose / Behave)
1660316607
if hasattr(self, "_xvfb_display") and self._xvfb_display:
16608+
# Stop the Xvfb virtual display launched from BaseCase
1660416609
try:
1660516610
if hasattr(self._xvfb_display, "stop"):
1660616611
self._xvfb_display.stop()
@@ -16610,6 +16615,20 @@ def tearDown(self):
1661016615
pass
1661116616
except Exception:
1661216617
pass
16618+
if (
16619+
hasattr(sb_config, "_virtual_display")
16620+
and sb_config._virtual_display
16621+
and hasattr(sb_config._virtual_display, "stop")
16622+
):
16623+
# CDP Mode may launch a 2nd Xvfb virtual display
16624+
try:
16625+
sb_config._virtual_display.stop()
16626+
sb_config._virtual_display = None
16627+
sb_config.headless_active = False
16628+
except AttributeError:
16629+
pass
16630+
except Exception:
16631+
pass
1661316632
if self.__visual_baseline_copies:
1661416633
sb_config._visual_baseline_copies = True
1661516634
if has_exception:

‎seleniumbase/plugins/sb_manager.py

+13
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,19 @@ def SB(
12561256
print(traceback.format_exc().strip())
12571257
if test and not test_passed:
12581258
print("********** ERROR: The test AND the tearDown() FAILED!")
1259+
if (
1260+
hasattr(sb_config, "_virtual_display")
1261+
and sb_config._virtual_display
1262+
and hasattr(sb_config._virtual_display, "stop")
1263+
):
1264+
try:
1265+
sb_config._virtual_display.stop()
1266+
sb_config._virtual_display = None
1267+
sb_config.headless_active = False
1268+
except AttributeError:
1269+
pass
1270+
except Exception:
1271+
pass
12591272
end_time = time.time()
12601273
run_time = end_time - start_time
12611274
sb_config = sb_config_backup

‎seleniumbase/undetected/cdp_driver/cdp_util.py

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ def __activate_virtual_display_as_needed(
8484
"\nX11 display failed! Will use regular xvfb!"
8585
)
8686
__activate_standard_virtual_display()
87+
else:
88+
sb_config._virtual_display = _xvfb_display
89+
sb_config.headless_active = True
8790
except Exception as e:
8891
if hasattr(e, "msg"):
8992
print("\n" + str(e.msg))

0 commit comments

Comments
 (0)