14

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.

Nikunj Aggarwal
  • 754
  • 2
  • 12
  • 32
  • 1
    Possible duplicate of [org.openqa.selenium.WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser](https://stackoverflow.com/questions/50642308/org-openqa-selenium-webdriverexception-unknown-error-devtoolsactiveport-file-d) – Nakilon Aug 15 '18 at 13:39
  • 2
    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 xvfb – sdfgeoff Sep 06 '18 at 14:32
  • @sdfgeoff where to add export DISPLAY=:0? – ira Nov 01 '22 at 11:20

17 Answers17

12

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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    We further debug the issue, and problems look a bit different. if I am launching google-chrome-stable through ssh it does'nt launch the chrome. but if we take remote of same machine using remmina and then use the terminal to launch the chrome, it launches it successfully. – Nikunj Aggarwal Jun 11 '18 at 08:53
  • Hm. Inside a Docker container and running as a normal [USER](https://docs.docker.com/engine/reference/builder/#user) I still see the above error. Using the `--no-sandbox` argument in addition to `--headless` worked – Jens Nov 27 '21 at 07:27
3

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)
Alex Montoya
  • 4,697
  • 1
  • 30
  • 31
3

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
desertnaut
  • 57,590
  • 26
  • 140
  • 166
3

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

crifan
  • 12,947
  • 1
  • 71
  • 56
2

Try to run selenium-server without sudo-privileges:

java -jar path/to/selenium-server-standalone.jar
Community
  • 1
  • 1
fpsthirty
  • 185
  • 1
  • 4
  • 15
1

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.

Vincent Gerris
  • 7,228
  • 1
  • 24
  • 22
  • Headless isn't a viable option if you want to save screenshots, still, my wdio won't start even as headless – Sam Lahm Jun 29 '21 at 07:38
  • So don't use headless but another mentioned option and use mentioned versions to be sure. don't know what you mean with wdio, but I suggest you post a new question if you have a concrete error based on 'not starting'. you should get a concrete error – Vincent Gerris Jul 03 '21 at 23:08
1

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
xdetector
  • 31
  • 4
1

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.

Sean Griffin
  • 111
  • 1
  • 8
1

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

Sardar Faisal
  • 594
  • 5
  • 20
0

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.

S.K. Venkat
  • 1,749
  • 2
  • 23
  • 35
  • Hey, how can I set the env variable DISPLAY to existing session? Is this done in the python file or in terminal? Thank you. – Rehan Mahmood Nov 26 '20 at 08:23
  • You can set the env variable DISPLAY from both python script and the terminal. But the recommended way is to set it from terminal before starting the python script. – S.K. Venkat Nov 30 '20 at 03:08
0

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.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
DiegoZ
  • 1
0

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.

Tom Hebb
  • 991
  • 1
  • 8
  • 14
0

I encountered this when running gitlab-runner. At the main display console, run xhost + to allow remote display access:

xhost +

And in .gitlab-ci.yml, I set the DISPLAY (assuming it's :10.0):

export DISPLAY=:10.0
0

For me, the issue was that the /tmp/Crashpad folder was owned by root and could not be written by my user (centos). I did the following and all was ok:

chmod 664 -R /tmp/Crashpad

I manged to debug this by trying to run the google-chrome binary directly from the terminal as the user I was trying to run selenium with (centos).

Doing that, I got the following error which helped me debug:

[19:11][centos@my.computer.com ~]$ /usr/bin/google-chrome --headless
[0110/191116.894836:ERROR:filesystem_posix.cc(63)] mkdir /tmp/Crashpad/new: Permission denied (13)
Patrick
  • 2,769
  • 2
  • 25
  • 29
0

solved by uninstall dbus conda remove dbus

leo
  • 91
  • 1
  • 1
  • 4
0

For GitHub Actions

For anyone running into this error while running tests on their CI/CD environment (GitHub Actions in my case), I was getting this because I was trying to replace the GitHub runner's default version of Chrome with an earlier version in order to fix this issue. I eventually replaced my rm and mv commands with a simple ln command to symbolically link the default Chrome binary (located at usr/bin/google-chrome) to my newly-downloaded one, and that works perfectly.gets my builds

Here is the relevant part of my GitHub Actions workflow:

  - name: Downgrade Chrome browser to v114
    uses: browser-actions/setup-chrome@v1
    with:
      chrome-version: 1134343 # Last commit number for Chrome v114
    id: setup-chrome
  - run: sudo ln -fs ${{ steps.setup-chrome.outputs.chrome-path }} /usr/bin/google-chrome
  - name: Downgrade Chrome driver to v114
    run: php artisan dusk:chrome-driver `/usr/bin/google-chrome --version | cut -d " " -f3 | cut -d "." -f1`
Hashim Aziz
  • 4,074
  • 5
  • 38
  • 68
-1

Simply use headless mode

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
driver = new ChromeDriver(options);
Isuru Dilshan
  • 719
  • 9
  • 18