0

I've upgraded Ubuntu to 20.04. It seems that Chrome isn't available as APT package but via snap. After the upgrade I'm getting the error while trying to instantiate Chrome browser:

>>> from selenium.webdriver import Chrome
>>> from selenium.webdriver.chrome.options import Options
>>> o = Options()
>>> o.headless = True
>>> o.add_argument('--disable-dev-shm-usage')
>>> o.add_argument('--no-sandbox')
>>> Chrome(options=o)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/var/www/order/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
    RemoteWebDriver.__init__(
  File "/var/www/order/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/var/www/order/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/var/www/order/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/var/www/order/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist

chromedriver version is 87.0.4280.20 chromium-browser version is Chromium 87.0.4280.66 snap

I read this discussion. The case is however isn't same. I run python as a regular user. However I've disabled dev-shm-usage and sandbox. But still it doesn't work. It worked before I've upgraded to Ubuntu 20.04. So I assume it has something to do with snap version of Chrome.

I have found out following configuration works:

Start first Chrome chromium-browser --headless --remote-debugging-port=9222 and then:

>>> from selenium.webdriver import Chrome
>>> from selenium.webdriver.chrome.options import Options
>>> o = Options()
>>> o.add_experimental_option('debuggerAddress', 'localhost:9222')
>>> b = Chrome(options=o)
>>> b.get('https://google.com')
>>> b.title
'Google'
>>>

So it seems the problem is with starting the browser.

Opened a bug report

Ralfeus
  • 845
  • 9
  • 31

3 Answers3

3

Good explanation was provided by ChromeDriver development team in a response to the ticket I've submitted:

ChromeDriver uses the /tmp directory to communicate with Chromium, but Snap remaps /tmp directory to a different location (specifically, to /tmp/snap.chomium/tmp). This causes errors because ChromeDriver can't find files created by Chromium. ChromeDriver is designed and tested with Google Chrome, and it may have compatibility issues with third-party distributions.

There are a couple of workarounds:

  1. Tell ChromeDriver to use a location in your home directory, instead of /tmp. For example, if your home directory is /home/me, you can add the following line of code to your script:
    o.add_argument('--user-data-dir=/home/me/foo')

  2. Explicitly select a port for ChromeDriver to communiate with Chromium. You need to carefully select a port (e.g., 9222) that isn't already in use, and then add the following line to your script:
    o.add_argument('--remote-debugging-port=9222')

and

Another (probably better) workaround is to use the ChromeDriver installed by Snap, which is at /snap/bin/chromium.chromedriver. E.g., driver = Chrome('/snap/bin/chromium.chromedriver', options=o)

Since this version of ChromeDriver runs inside Snap, its /tmp directory is redirected in the same way as Chromium

Ralfeus
  • 845
  • 9
  • 31
-1

The solution to my problem is rather workaround but nevertheless it might be useful. I have downgraded to Chrome version 86, which I have installed from deb package instead of snap. Installation instructions can be found here

Once I've downgraded chromium and chromedriver to version 86 the problem has gone.

Ralfeus
  • 845
  • 9
  • 31
-1

The answer provided by Ralfeus is wholesome, I am using Ubuntu 20.04 and was facing same issues.

I used driver = Chrome('/snap/bin/chromium.chromedriver') , yeah without any additional options and it worked.

Abhineet
  • 41
  • 4