8
votes

I have an ubuntu server having the UI as well. U can execute the test cases by firing mvn test command. But the problem is when I do ssh of the machine through the terminal from another machine I get the following error-

unknown error: DevToolsActivePort file doesn't exist
  (Driver info: chromedriver=2.40.565383 (76257d1ab79276b2d53ee976b2c3e3b9f335cde7),platform=Linux 4.4.0-121-generic x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 60.04 seconds
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T18:33:54.468Z'
System info: host: 'ubuntu-test', ip: 'X.X.X.X', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-121-generic', java.version: '1.8.0_171'
Driver info: driver.version: ChromeDriver

but the same command starts chrome successfully if I take remote of the machine through remmina and then execute the same command of this machines terminal.

13
In my case this was caused by Chrome not being able to access an X display. This was solved by adding export DISPLAY=:0 to the command running chromedriver. In other cases, it could be solved with xvfbsdfgeoff

13 Answers

7
votes

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.


This error message...

unknown error: DevToolsActivePort file doesn't exist

...implies that the ChromeDriver was unable to initiate/spawn a new WebBrowser i.e. Chrome Browser session.

Your code trials and the versioning information of all the binaries would have given us some hint about what's going wrong.

However as per Add --disable-dev-shm-usage to default launch flags seems adding the argument --disable-dev-shm-usage will temporary solve the issue.

If you desire to initiate/span a new Chrome Browser session you can use the following Java solution:

System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
options.addArguments("start-maximized"); // open Browser in maximized mode
options.addArguments("disable-infobars"); // disabling infobars
options.addArguments("--disable-extensions"); // disabling extensions
options.addArguments("--disable-gpu"); // applicable to windows os only
options.addArguments("--no-sandbox"); // Bypass OS security model
WebDriver driver = new ChromeDriver(options);
driver.get("https://google.com");

disable-dev-shm-usage

As per base_switches.cc disable-dev-shm-usage seems to be valid only on Linix OS:

#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
// The /dev/shm partition is too small in certain VM environments, causing
// Chrome to fail or crash (see http://crbug.com/715363). Use this flag to
// work-around this issue (a temporary directory will always be used to create
// anonymous shared memory files).
const char kDisableDevShmUsage[] = "disable-dev-shm-usage";
#endif

In the discussion Add an option to use /tmp instead of /dev/shm David mentions:

I think it would depend on how are /dev/shm and /tmp mounted. If they are both mounted as tmpfs I'm assuming there won't be any difference. if for some reason /tmp is not mapped as tmpfs (and I think is mapped as tmpfs by default by systemd), chrome shared memory management always maps files into memory when creating an anonymous shared files, so even in that case shouldn't be much difference. I guess you could force telemetry tests with the flag enabled and see how it goes.

As for why not use by default, it was a pushed back by the shared memory team, I guess it makes sense it should be useing /dev/shm for shared memory by default.

Ultimately all this should be moving to use memfd_create, but I don't think that's going to happen any time soon, since it will require refactoring Chrome memory management significantly.


Reference

You can find a couple of detailed discussions in:


Outro

Here is the link to the Sandbox story.

2
votes

I had a similar issue when I was trying to selenium UI test cases in headless mode. This occurred as I did not have a display server. Starting Xvfb worked for me.

sudo yum -y install Xvfb libXfont Xorg

sudo yum -y groupinstall "X Window System" "Desktop" "Fonts" "General Purpose Desktop"

Xvfb :99 -screen 0 1024x768x24 &

export DISPLAY=:1

2
votes

Try to run selenium-server without sudo-privileges:

java -jar path/to/selenium-server-standalone.jar
1
votes

I faced the same problem when I run selenium with cron job. And after long suffering I found the way to solve it. Just add this line to the beginning of the shell script:

export DISPLAY=:1
1
votes

If you run from ssh without an X-forward your chrome browser will crash. To prevent that you can use the options DebanjanB posted, the most important being --headless. If running as root ( not recommended ) you need --no-sandbox as well.

I also had this error when I used al older version of selenium-java (3.5.3) with the newer chromedriver (75.x). It worked for me to use the 2.46 version of the chromedriver with the 3.5.3, or the 75.x with 3.141.59 of selenium java.

Running a local Xvfb should work too, but I suggest to use headless, it can be much faster.

Check the suggested duplicate post too and please update and mark as solved whatever helped you.

1
votes

None of the above worked. Only solution was to use the driver from the below location:

'/snap/bin/chromium.chromedriver'

This question explains it: https://stackoverflow.com/a/61980562/1568464

0
votes

I use this configuration using python

import os
from selenium import webdriver 
from selenium.webdriver.chrome.options import Options 

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("no-sandbox")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--headless")
driver = os.path.join("path/of/driver","chromedriver")

browser = webdriver.Chrome(executable_path=driver,chrome_options=chrome_options)
browser.get("https://www.example.com")
print(browser.title)
0
votes

I had been receiving this error specifically in chrome embedded applications with Selenium, like CEF or electron apps.

Using the arguments --headless and --no-sandbox and --disable-gpu was not a solution.

The cause of my issue was the electron and CEF applications. They were not forwarding all the chrome command line switches onto the running Chrome instance inside them and as a result the DevToolActivePort file was not being created.

I have published a manual process to follow in my answer to another similar question here -> https://stackoverflow.com/a/62545820/8708890. You can follow this manual process and see if it fixes your issue.

0
votes

It happening to me using headless chrome driver and trying to set window size to 1366x768 or 1600x900. I could only fix it by returning to 1920x1080.

Hope it helps to somebody

0
votes

Try this method to instantiate chrome web driver could help you to overcome this issue in ubuntu :

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

opt = Options()
opt.add_argument("--no-sandbox")
opt.add_argument("--disable-dev-shm-usage")

driver = webdriver.Chrome(chrome_options=opt, 
    executable_path='<your-chromedriver-path>')
driver.get('https://www.google.com/')

Thanks to @George Pantazes for his comment for the answer

And make sure the env variable DISPLAY has been set to existing session in the terminal where you'll start the chrome browser.

0
votes

I was running it as a GitHub Action, but didn't add options.addArguments("--headless"). Once I put that in, the fault went away. The various other suggestions in this thread were to no avail.

I know this is my stupidity, but I thought that it could be helpful to come clean about it, because the link between cause and effect in this case was not immediately clear.

0
votes

as @DebanjanB answer is correct, but not clear.

After fixed issue and summary solution to here:

Issue: run selenium, but error

code:

from selenium import webdriver
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--headless’)
driver = webdriver.Chrome(options=chromeOptions)

run but error:

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

Cause

  • direct cause

    • unknown error: DevToolsActivePort file doesn't exist means ChromeDriver spawn WebBrowser (Chrome Browser session) failed
  • root cause

    • use ROOT to run Chrome, will cause this error

Solution

(recommended) correct solution

use NORMAL USER to run Chrome

how to use normal user to run chrome?

my case: in CentOS

(1) change chrome binary to own by normal user

sudo chown limao:limao /opt/google/chrome/google-chrome

note: here chrome binary is /usr/bin/google-chrome after several soft link to real one is /opt/google/chrome/google-chrome

after change owner:

limao@localhost:~/dev/ShortLinkParseServer/logs    $ ll /usr/bin/google-chrome
lrwxrwxrwx 1 root root 31 Aug  4 16:30 /usr/bin/google-chrome -> /etc/alternatives/google-chrome
limao@localhost:~/dev/ShortLinkParseServer/logs    $ ll /etc/alternatives/google-chrome 
lrwxrwxrwx 1 root root 29 Aug  4 16:30 /etc/alternatives/google-chrome -> /usr/bin/google-chrome-stable
limao@localhost:~/dev/ShortLinkParseServer/logs    $ ll /opt/google/chrome/google-chrome 
-rwxr-xr-x 1 limao limao 1.9K Jul 31 04:46 /opt/google/chrome/google-chrome

(2) set normal user for supervisord spawn program

add user=xxx into your supervisord config file, like this:

$ cat /etc/supervisord.d/supervisord_ShortLinkParseServer.conf 

[program:ShortLinkParseServer]
command=/xxx/gunicorn/gunicorn_config.py app:app
...
; use normal user instead default root use, to avoid later chrome exception: unknown error: DevToolsActivePort file doesn't exist
user=limao
...

workaround solution (to be verified)

add flag --disable-dev-shm-usage

chromeOptions.add_argument('--disable-dev-shm-usage')

wrong solution

too much people used this: add flag --no-sandbox

but --no-sandbox just means not use sandbox

-> true effect is just bypass OS security model

-> so highly NOT recommend to use --no-sandbox

-1
votes

Simply use headless mode

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
driver = new ChromeDriver(options);