0

I want to run selenium through chromium. I wrote this code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument("--disable-gpu")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
options.binary_location = "/snap/bin/chromium"
driver = webdriver.Chrome(chrome_options=options)

But this code throws an error:

selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist
Stacktrace:
#0 0x55efd7355a23 <unknown>
#1 0x55efd6e20e18 <unknown>
#2 0x55efd6e46e12 <unknown>

The chromodriver of the correct version is in usr/bin. What am I doing wrong?

undetected Selenium
  • 151,581
  • 34
  • 225
  • 281
Dima
  • 9
  • 5
  • If the chrome driver is in `/usr/bin`, then why are you telling it `/snap/bin/chromium`? – Tim Roberts Jan 23 '22 at 19:46
  • By default selenium runs google chrome. From this answer https://stackoverflow.com/questions/49298245/selenium-python-with-chromium-browser-windows, I see that this is how chromium is launched, but I did not find analogues with Linux, in all questions they forward the path to the chromium exe – Dima Jan 23 '22 at 19:54
  • There are some similar questions. It appears to be incompaibility between the ChromeDriver and the actual Chromium instance. https://stackoverflow.com/questions/50642308/webdriverexception-unknown-error-devtoolsactiveport-file-doesnt-exist-while-t – Tim Roberts Jan 23 '22 at 20:04

3 Answers3

0

Thumb rule

A common cause for Chrome to crash during startup is running Chrome as root user (administrator) on Linux. While it is possible to work around this issue by passing --no-sandbox flag when creating your WebDriver session, such a configuration is unsupported and highly discouraged. You need to configure your environment to run Chrome as a regular user instead.

However you need to take careof a couple of things:

  • --disable-gpu was related to , so you need to drop it.

  • chrome_options is deprecated, use options instead.

    driver = webdriver.Chrome(options=options)
    

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 151,581
  • 34
  • 225
  • 281
0

I solved the problem by reinstalling chromium through apt sudo apt install chromium-browser (before that it was installed through snap). My working code looks like this

options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
if headless:
options.add_argument('--headless')
options.binary_location = "/usr/bin/chromium-browser"
self.driver = webdriver.Chrome(options=options)
Dima
  • 9
  • 5
0

I was getting this error after upgrading my chromedriver version to 86 and Python runtime to 3.8 from 3.6 on AWS Lambda (Amazon Linux 2) run in a docker container. I played whack a mole for hours with chrome/chromedriver starting issues.

Eventually I found this actively maintained min miplementation of python+selenium+docker. https://github.com/umihico/docker-selenium-lambda/ The setup in their Dockerfile and test.py chrome_options worked.

Kyle
  • 303
  • 1
  • 14