Selenium: Chrome failed to start: exited abnormally

Question:

I’m trying to make an application with python and selenium but I run into an error.
I already searched for a solution and found out that you should add those arguments: --no-sandbox, --headless, --disable-dev-shm-usage which I did but it still didn’t work. The error (full traceback below): selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.

This happens on the initialization of the driver.

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome("/usr/bin/chromedriver", options=options)

# ...    

Ubuntu Server: 22.04
Python: 3.8
selenium: 4.3.0
Google Chrome: 104.0.5112.79 (https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb)
Chromedriver: 104.0.5112.79 (https://chromedriver.storage.googleapis.com/104.0.5112.79/chromedriver_linux64.zip)

Location of google-chrome: /usr/bin/google-chrome
Location of chromedriver: /usr/bin/chromedriver

Full traceback:

Traceback (most recent call last):
  File "/home/admini/websites/myapp/./wsgi.py", line 1, in <module>
    from main import app
  File "/home/admini/websites/myapp/./main.py", line 92, in <module>
    sel_driver = SeleniumWebdriver()
  File "/home/admini/websites/myapp/./main.py", line 84, in __init__
    self.driver = init_driver()
  File "/home/admini/websites/myapp/./main.py", line 74, in init_driver
    driver = webdriver.Chrome("/usr/bin/chromedriver", options=options)
  File "/home/admini/websites/myapp/venv/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
    super().__init__(DesiredCapabilities.CHROME['browserName'], "goog",
  File "/home/admini/websites/myapp/venv/lib/python3.8/site-packages/selenium/webdriver/chromium/webdriver.py", line 92, in __init__
    super().__init__(
  File "/home/admini/websites/myapp/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 277, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/admini/websites/myapp/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 370, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/admini/websites/myapp/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 435, in execute
    self.error_handler.check_response(response)
  File "/home/admini/websites/myapp/venv/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
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.)

Edit

In addition to this code it’s hosting a flask app for a website which runs with uwsgi and nginx. I noticed when I run the script, it works. However it doesn’t when I run it as a service with uwsgi.

myapp.ini

[uwsgi]
module = wsgi:app

master = true
processes = 5

socket = myapp.sock
chmod-socket = 660
vacuum = true

die-on-term = true

#location of log files
logto = /var/log/uwsgi/%n.log

/etc/systemd/system/myapp.service

[Unit]
Description=uWSGI instance to serve myapp
After=network.target

[Service]
User=admini
Group=www-data
WorkingDirectory=/home/admini/websites/myapp
Environment="PATH=/home/admini/websites/myapp/venv/bin"
ExecStart=/home/admini/websites/myapp/venv/bin/uwsgi --ini myapp.ini

[Install]
WantedBy=multi-user.target
Asked By: goeadli

||

Answers:

From https://stackoverflow.com/a/70265091/13508045

Add :/usr/bin:/bin to your PATH as shown below:

Environment="PATH=/home/admini/websites/myapp/venv/bin:/usr/bin:/bin"

Complete file looks like this:

[Unit]
Description=uWSGI instance to serve myapp
After=network.target

[Service]
User=admini
Group=www-data
WorkingDirectory=/home/admini/websites/myapp
Environment="PATH=/home/admini/websites/myapp/venv/bin:/usr/bin:/bin"
ExecStart=/home/admini/websites/myapp/venv/bin/uwsgi --ini myapp.ini

[Install]
WantedBy=multi-user.target
Answered By: puncher