WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist error using ChromeDriver Chrome and user-data-dir

Question:

I am trying to access some websites with different accounts, and to avoid logging in every time I am planning to make an user profile for each one. I am hosting my Python app on an Ubuntu server, so I have to run it headless, and I also need the mobile emulation. Here’s a sample of what I’m trying to do:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_experimental_option("mobileEmulation", {"deviceName": "Pixel 2"})
options.add_argument("user-data-dir=account-data/account_3")
chromedriver = "./webdrivers/chrome"
driver = webdriver.Chrome(executable_path=chromedriver, options=options)

print("START SEARCH")
# Google is an example, but nothing works
driver.get("https://www.google.com")
print("FINISHED")

I see START SEARCH but never FINISHED. When I comment the user-data-dir options, however, everything works perfectly. The weird thing is that this happens on an Ubuntu 20 LTS server, but in my Mac it has no issues.


While trying to get the versions for this question, out of the blue the version that doesn’t work started giving me this:

Traceback (most recent call last):
  File "seleniumtest.py", line 12, in <module>
    driver = webdriver.Chrome(executable_path=chromedriver, options=options)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.7/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
  (Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 5.4.0-1009-kvm x86_64)

The only thing I’ve really done before getting this error is deleting all the /tmp/.org.chromium.Chromium.* files but those should only be temporary files and new ones should be created all the time when I don’t specify the user-data-dir flag.

Asked By: TheSartorsss

||

Answers:

I don’t see any major issue in your code block.

However chromedriver=2.41.578700 is pretty old and ancient.


Solution

Ensure that:

  • Selenium is upgraded to current levels Version 3.141.59.
  • ChromeDriver is updated to current ChromeDriver v83.0 level.
  • Chrome is updated to current Chrome Version 83.0 level. (as per ChromeDriver v83.0 release notes)
  • If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client.
  • Take a System Reboot.
  • Execute your @Test as non-root user.
  • Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.
Answered By: undetected Selenium

Instead of all your code try this, first download chromedriver, and install it. Then put that path into the line below.

Remember, when you execute the script below, a new chrome window will be opened but do not close it.

from selenium import webdriver

driver = webdriver.Chrome(executable_path=r'C:\Users\sonal\.wdm\drivers\chromedriver\80.0.3987.106\win32\chromedriver.exe')
driver.get("https://www.google.com")
Answered By: Sonal Savaliya

Turns out the problem was that I did not put an absolute path in the user-data-dirflag.

from selenium import webdriver
import os

options = webdriver.ChromeOptions()
path = os.path.abspath("account-data/account_3")
options.add_argument("user-data-dir=" + path)
# other options...
chromedriver = "./webdrivers/chrome"
driver = webdriver.Chrome(executable_path=chromedriver, options=options)

# do stuff...
driver.close()
Answered By: TheSartorsss

For Mac users (M1, M2) cores –

use seleniarm/standalone-chromium instead of selenium/standalone-chrome

Answered By: OLS