Skip to content
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

Headed Mode with CDP Mode Falls Back to xvfb on Subsequent Iterations in Loop #3389

Open
JustusLatzer opened this issue Jan 5, 2025 · 3 comments
Assignees
Labels
bug Uh oh... Something needs to be fixed UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode

Comments

@JustusLatzer
Copy link

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!

@mdmintz mdmintz added bug Uh oh... Something needs to be fixed UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode labels Jan 5, 2025
@mdmintz
Copy link
Member

mdmintz commented Jan 5, 2025

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:

cdp_util.start(host=cdp_host, port=cdp_port)

which will get used in the start() method located in cdp_util.py:

async def start(
config: Optional[Config] = None,
*,
user_data_dir: Optional[PathLike] = None,
headless: Optional[bool] = False,

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.

@mdmintz mdmintz self-assigned this Jan 5, 2025
@JustusLatzer
Copy link
Author

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!

@mdmintz
Copy link
Member

mdmintz commented Jan 6, 2025

I see how to fix that too for the SB() manager. It'll be included with this ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Uh oh... Something needs to be fixed UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode
Projects
None yet
Development

No branches or pull requests

2 participants