I've got a project that uses the Chrome webdriver with Selenium and came across an intermittent DevToolsActivePort issue that prevented the webdriver from starting. As usual, it was a simple problem, but it took a while to determine the cause.
TLDR: Try manually opening and closing Chrome. Check your script's shutdown process.
Software Versions:
Message: unknown error: Chrome failed to start: exited normally.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /Applications/Google Chrome.app/Contents/MacOS/Google Chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Here is a snippet of the driver initialization:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
prefs = {
"download.default_directory": Download_dir,
'download.prompt_for_download': False,
'directory_upgrade': True,
'safebrowsing.enabled': True,
'profile.default_content_setting_values.automatic_downloads': 1,
'profile.default_content_settings.popups': 0,
}
options.add_argument("--headless=new")
options.add_argument("--window-size=1920,1080")
options.add_argument("--disable-extensions")
options.add_argument("--user-data-dir=/Users/<username>/Library/Application Support/Google/Chrome/")
options.add_argument('--profile-directory=Profile 3') # Profile Folder Name
options.binary_location = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
I saw a lot of posts suggesting to add some options to the driver such as --no-sandbox, --disable-dev-shm-usage which both remove security layers, not ideal, and also did not resolve the issue in my case. I also saw a suggestion to add the --remote-debugging-port=<example port> option to manually choose the port. This is probably the cause of the issue, but I could not find the correct port to use.
I ended up solving the issue by simply opening and quitting Chrome before starting the script. I believe the error was caused by the script not properly closing the Chrome instance which may have caused the debugging port mismatch. By opening and closing Chrome manually, I could ensure a proper shutdown, and the selenium fired right up.
I have now added driver.close() / driver.quit() to the keyboard interrupt block and it seems to have resolved the issue.
Hope this helps the other noobs out there.
Initializing the Chrome webdriver in Selenium.
Selenium initializes Chrome webdriver.
Occasionally would fail unknown error: DevToolsActivePort file doesn't exist error.
If you run your tests on a server, it might help to set the DISPLAY variable to something (any nonempty value?). Even if Chrome runs in --headless mode.
I got these crashes while connecting to a Linux server (which I wanted to run my PHP/Codeception tests on) with ssh <servername> and then both Chrome and Chromium failed to start (to run tests in headless mode), resulting in
unknown error: DevToolsActivePort file doesn't exist.
Connecting with ssh -Y <servername> helped to get rid of the errors. My $DISPLAY variable on my ssh terminal was set to e.g. localhost:11.1 and that was sufficient.
A nonexisting DISPLAY variable led to errors.
(I haven't investigated this further).
(I just realized you run tests on a Mac, so my advice might not apply, but perhaps it helps someone else)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With