unknown error: DevToolsActivePort file doesn't exist error while executing Selenium UI test cases on ubuntu
Asked Answered
A

17

16

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.

Alphabetic answered 11/6, 2018 at 4:56 Comment(3)
Possible duplicate of org.openqa.selenium.WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome BrowserBryon
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 xvfbJosephinajosephine
@Josephinajosephine where to add export DISPLAY=:0?Plauen
T
13

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.

Torrie answered 11/6, 2018 at 6:22 Comment(2)
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.Alphabetic
Hm. Inside a Docker container and running as a normal USER I still see the above error. Using the --no-sandbox argument in addition to --headless worked 🤔Armed
A
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)
Autocrat answered 27/9, 2018 at 22:9 Comment(0)
N
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
Noenoel answered 19/12, 2018 at 4:39 Comment(0)
J
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

Jesuitism answered 5/8, 2021 at 3:55 Comment(0)
T
2

Try to run selenium-server without sudo-privileges:

java -jar path/to/selenium-server-standalone.jar
Trioecious answered 15/1, 2019 at 10:26 Comment(0)
D
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.

Drucilladrucy answered 13/6, 2019 at 21:48 Comment(2)
Headless isn't a viable option if you want to save screenshots, still, my wdio won't start even as headlessFoghorn
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 errorDrucilladrucy
F
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
Fabe answered 21/3, 2020 at 10:41 Comment(0)
C
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://mcmap.net/q/67418/-showing-error-unknown-error-devtoolsactiveport-file-doesn-39-t-exist-it-39-s-electron-application-i-am-using-windows-os. You can follow this manual process and see if it fixes your issue.

Curtate answered 24/6, 2020 at 2:8 Comment(0)
C
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://mcmap.net/q/66848/-webdriverexception-unknown-error-devtoolsactiveport-file-doesn-39-t-exist-while-trying-to-initiate-chrome-browser

Coltin answered 1/2, 2021 at 14:8 Comment(0)
A
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.

Alsup answered 24/12, 2019 at 10:31 Comment(2)
Hey, how can I set the env variable DISPLAY to existing session? Is this done in the python file or in terminal? Thank you.Shaughnessy
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.Alsup
T
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.

Tortoiseshell answered 30/6, 2020 at 12:37 Comment(2)
Probably could be a comment.Lynn
my answer? I tried, but I cannot put comments as I have no reputationTortoiseshell
S
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.

Supereminent answered 26/11, 2020 at 10:6 Comment(0)
D
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
Delorenzo answered 6/9, 2021 at 3:27 Comment(0)
C
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][[email protected] ~]$ /usr/bin/google-chrome --headless
[0110/191116.894836:ERROR:filesystem_posix.cc(63)] mkdir /tmp/Crashpad/new: Permission denied (13)
Carlsen answered 10/1, 2023 at 19:15 Comment(0)
T
0

solved by uninstall dbus conda remove dbus

Tonsorial answered 22/5, 2023 at 6:16 Comment(0)
R
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`
Rives answered 29/8, 2023 at 19:23 Comment(0)
R
-1

Simply use headless mode

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
driver = new ChromeDriver(options);
Renteria answered 2/12, 2019 at 2:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.