SeleniumBase
SeleniumBase copied to clipboard
Headed Mode with CDP Mode Falls Back to xvfb on Subsequent Iterations in Loop
I am using the latest version of sb (v4.33.12), Python 3.13.1 on Linux Fedora 41 (Wayland, I did install xorg-x11-server-Xvfb) and I am encountering an issue while using SeleniumBase in CDP mode with headed=True and xvfb=False. My goal is to see the browser automation happening during each iteration of a loop. Below is the structure of my code:
for _ in range(12):
with SB(uc=True, headed=True, browser="chrome", xvfb=False) as sb:
sb.activate_cdp_mode("https://example.com")
# do stuff
In the first iteration, everything works perfectly:
The browser opens visibly (not in a virtual display). It navigates to the website and performs the actions as expected. Upon exiting the context manager, the browser process terminates correctly (confirmed via htop).
However, starting from the second iteration, the behavior changes:
The browser runs in the xvfb virtual display instead of being visible, despite headed=True and xvfb=False being explicitly set. To verify this, I added a print(sb.xvfb) statement immediately after entering the context manager, which consistently outputs False.
This behavior does not change even when using "Driver" instead of "SB".
Interestingly, the issue does resolve when I avoid using CDP mode and use uc_open_with_reconnect and sb.click() or sb.uc_click() instead of sb.cdp.click() instead:
for _ in range(12):
with SB(uc=True, headed=True, browser="chrome", xvfb=False) as sb:
sb.uc_open_with_reconnect("https://example.com")
# do stuff
In this case, the browser remains visible on every iteration, and xvfb is correctly not used.
It seems there is something related to cdp not torn down correctly as using a normal sb.open() before the sb.activate_cdp_mode does not change anything about Chrome starting up in the xvfb display after the first iteration.
Just to clarify, I am not multithreading this script, issues related to this can be excluded.
Let me know if you need more details!
I see the issue. CDP Mode activates the virtual display by default on Linux after the first browser has been launched. (Whenever the virtual display is running, any new browser window will launch inside there, invisible.)
I can fix this in SB() by passing additional parameters into start() from browser_launcher.py:
https://github.com/seleniumbase/SeleniumBase/blob/35dcd8214325a9b54c1d8ab67eb6f26544c2266c/seleniumbase/core/browser_launcher.py#L539
which will get used in the start() method located in cdp_util.py:
https://github.com/seleniumbase/SeleniumBase/blob/35dcd8214325a9b54c1d8ab67eb6f26544c2266c/seleniumbase/undetected/cdp_driver/cdp_util.py#L138-L142
Since the browser has already been launched by the time the start() method is called, I only need to pass parameters related to determining whether or not Xvfb needs to be activated.
Also, since the CDP-Driver can be used standalone, most parameters listed in start() don't get used unless calling the CDP-Driver directly.
Hey @mdmintz !
I also want to add, that the Xvfb processes do not exit automatically but keep accumulating in the background. As a quick and dirty workaround I am using subprocess.run(["killall", "Xvfb"], check=True) but of course that is not ideal.
Thanks for your work and the quick reply, I hope that helps!
I see how to fix that too for the SB() manager. It'll be included with this ticket.
This was resolved in 4.33.13 - https://github.com/seleniumbase/SeleniumBase/releases/tag/v4.33.13