selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist with chromium browser and Selenium Python
H

6

7

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 chromedriver of the correct version is in usr/bin. What am I doing wrong?

Hawkie answered 23/1, 2022 at 19:43 Comment(3)
If the chrome driver is in /usr/bin, then why are you telling it /snap/bin/chromium?Mishnah
By default selenium runs google chrome. From this answer #49298745, 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 exeHawkie
There are some similar questions. It appears to be incompaibility between the ChromeDriver and the actual Chromium instance. #50642808Mishnah
H
6

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)
Hawkie answered 24/1, 2022 at 13:46 Comment(2)
Are you sure the --headless argument isn't what fixed it?Afton
I'm using ubuntu 22.04 server. After similar same issues the following worked for me: "const service = new chrome.ServiceBuilder('./chromedriver'); const driver = new Builder().forBrowser('chrome').setChromeService(service).setChromeOptions(new chrome.Options().addArguments( ['--headless','--no-sandbox'])).build();" I systematically went through the other options and when I didn't have the headless and sandbox it broke.Toothpaste
M
8

This is because you have specified --user-data-dir and maybe a --profile-directory, and there is already a running Chrome instance which would have created a DevToolsActivePort in your specified user data dir. Thus selenium will be unable to spawn a new browser instance.

My solution is to copy the Default and Profile 1 folders (which are the two profiles I used in my case) under %LOCALAPPDATA%\Microsoft\Edge\User Data\ to my newly created D:\user_data, and specify --user-data-dir to be D:\user_data. This way I have a copy of user data for selenium only.

Hope this will solve your problem.

Moffatt answered 21/1, 2023 at 4:23 Comment(2)
This helped me track down my issue with my script when initially running it, and then having it run as a systemd service.Restricted
this was the exact problem I was having, when I noticed that it only crashed when an existing session was already openLeela
H
6

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)
Hawkie answered 24/1, 2022 at 13:46 Comment(2)
Are you sure the --headless argument isn't what fixed it?Afton
I'm using ubuntu 22.04 server. After similar same issues the following worked for me: "const service = new chrome.ServiceBuilder('./chromedriver'); const driver = new Builder().forBrowser('chrome').setChromeService(service).setChromeOptions(new chrome.Options().addArguments( ['--headless','--no-sandbox'])).build();" I systematically went through the other options and when I didn't have the headless and sandbox it broke.Toothpaste
E
4

I found the solution here: selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist with chromium browser and Selenium Python

In ubuntu, chromium is installed using snap by default, and it breaks selenium.

The following fixed the problem:

sudo snap remove chromium

Then installing the proprietary chrome from https://www.google.com/chrome/

(The official chromium deb package just contains a snap).

Earthshaker answered 15/11, 2022 at 9:47 Comment(1)
What if you run selenium+docker? I get the same error.Catharine
T
1

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:

Talapoin answered 23/1, 2022 at 19:51 Comment(3)
Although true, this doesn't answer the question.Mishnah
@TimRoberts Thnx for the feedback, updated the answer, feedback please...Talapoin
Is running as root also a problem if you have selenium+docker?Catharine
C
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.

Catcher answered 6/5, 2022 at 18:13 Comment(0)
A
0

Turning on logging helps:

import logging
logger = logging.getLogger('selenium')
handler = logging.FileHandler('log.txt')
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)

"Headless" was needed to run it as root. However, I had to add "no-sandbox" to get it to run as web-user. No clue why, but this worked for me:

options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument("--headless")
driver = webdriver.Chrome(options=options)
Asleep answered 24/1 at 18:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.