ponty / PyVirtualDisplay

Python wrapper for Xvfb, Xephyr and Xvnc
BSD 2-Clause "Simplified" License
714 stars 78 forks source link

Selenium and Chrome DevToolsActivePort file doesn't exist after 89/90 chrome invocations #92

Open isparks-tg opened 8 months ago

isparks-tg commented 8 months ago

I am not 100% sure this is a pyVirtualDisplay problem (could be chrome or underlying libraries) but the following code fails for me at a count of 90 when visible=0 and 89 when visible=1. If I don't create Display it will run to 99, so something about the virtual display seems to be involved (hence the bug report here).

from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.chrome.service import Service as ChromeService
from pyvirtualdisplay import Display

if __name__ == "__main__":
    display = Display(visible=0, size=(768, 1024))
    display.start()

    for i in range(100):
        print(i)

        options = ChromeOptions()
        driver = Chrome(
            options=options,
            service=ChromeService(executable_path="/usr/local/bin/chromedriver"),
        )

        driver.get("https://www.example.com/")

        driver.close()
        driver.quit()

    display.stop()

with traceback:

Traceback (most recent call last):
  File "/home/isparks/.config/JetBrains/PyCharm2023.3/scratches/chrome_test.py", line 16, in <module>
    driver = Chrome(
             ^^^^^^^
  File "/home/isparks/envs/nexus3.11/lib/python3.11/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
    super().__init__(
  File "/home/isparks/envs/nexus3.11/lib/python3.11/site-packages/selenium/webdriver/chromium/webdriver.py", line 56, in __init__
    super().__init__(
  File "/home/isparks/envs/nexus3.11/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 205, in __init__
    self.start_session(capabilities)
  File "/home/isparks/envs/nexus3.11/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 289, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/isparks/envs/nexus3.11/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 344, in execute
    self.error_handler.check_response(response)
  File "/home/isparks/envs/nexus3.11/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: DevToolsActivePort file doesn't exist

Some information:

selenium==4.14.0 pyvirtualdisplay==3.0

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.3 LTS
Release:    22.04
Codename:   jammy
$ python3 --version
Python 3.10.12
$ google-chrome --version
Google Chrome 120.0.6099.224 
$ chromedriver --version
ChromeDriver 120.0.6099.109 (3419140ab665596f21b385ce136419fde0924272-refs/branch-heads/6099@{#1483})

I know the above are not an exact match, I get the same with chrome 121 and a matching chromedriver.

DeviousStoat commented 8 months ago

I seem to be getting a similar issue. I started getting it after the latest update of xvfb so I am suspecting it is an issue with xvfb. Are you using the latest version in ubuntu too? Could you share your xvfb version (apt-cache policy xvfb | grep Installed)?

isparks-tg commented 8 months ago
$ apt-cache policy xvfb | grep Installed
Installed: 2:21.1.4-2ubuntu1.7~22.04.7
DeviousStoat commented 8 months ago

Well I can confirm that my issue or at least part of my issue was in the latest version of xvfb on ubuntu. I don't get any issue if I downgrade.

Since it is not available on the official repo anymore. I did:

wget -O /tmp/xvfb.deb http://launchpadlibrarian.net/702315346/xvfb_1.20.13-1ubuntu1~20.04.12_amd64.deb
apt install -y --allow-downgrades /tmp/xvfb.deb

But since I see you are on jammy, your last package is probably this instead:

http://launchpadlibrarian.net/702313861/xvfb_21.1.4-2ubuntu1.7~22.04.5_amd64.deb

If it works for you too it should confirm that we had the same issue and that there is something weird going on with the latest version of xvfb.

isparks-tg commented 8 months ago

Thanks for the pointers. Downgrading xvfb worked for visibility=0 so your diagnosis looks good but I still get same failure at 89 with visible=1 suggesting there is also an issue in xephyr? (Not that I need visibility=1 got my use case)

display.py backend selection:

            if self._visible:
                self._backend = "xephyr"
            else:
                self._backend = "xvfb"