Halmon
Halmon

Reputation: 1077

WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally. Breaks with uWSGI, Python, Flask, Selenium

I'm running a Flask application with uWSGI on an Ubuntu server and trying to use selenium with chromedriver. This is the error I see when I run sudo systemctl status myapp

    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.)

It doesn't appear to be a problem with my google-chrome/chromedriver version since I can manually create the same script in a Python3 shell and Selenium works perfectly without any errors. This leads me into thinking it's something with my uWSGI/socket setup.

myapp.ini:

[uwsgi]
module = myapp:app

master = true
processes = 5

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

die-on-term = true

/etc/systemd/system/myapp.service:

[Unit]
Description=uWSGI myapp
After=network.target

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

[Install]
WantedBy=multi-user.target

chromedriver version: 86.0.4240.22

google-chrome version 86.0.4240.75

selenium_scraper.py:

def get_selenium_data(query):
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from webdriver_manager.chrome import ChromeDriverManager

    # Prepare headless chrome driver
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-extensions")
    chrome_options.add_argument("--disable-gpu")
    chrome_options.add_argument("--disable-infobars")
    chrome_options.add_argument("--start-maximized")
    chrome_options.add_argument("--disable-dev-shm-usage")
    driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
    driver.set_window_size(500, 951)
    driver.get(query)

As you can see I've used --no-sandbox as well as all the other flags and it works in the python3 interpreter on Ubuntu. However when I try to restart my uWSGI server/socket it gives me the Chrome failed to start error. Any help would be greatly appreciated!!

Upvotes: 3

Views: 677

Answers (1)

Gao Pinker
Gao Pinker

Reputation: 11

You should add full PATH to the 'Environment' section, otherwise you are likely using the Chrome driver from /usr/bin/google-chrome:

[Service]
User=xxx
Group=ggg
WorkingDirectory=/home/xxx/proj/dir
Environment="PATH=/home/xxx/anaconda3/envs/proj/bin/**:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin**"
ExecStart=/path/to/you/executable --parameters

Compare that to my failed myapp.service file:

[Service]
User=xxx
Group=ggg
WorkingDirectory=/home/xxx/proj/dir
Environment="PATH=/home/xxx/anaconda3/envs/proj/bin/"
ExecStart=/path/to/you/executable --parameters

Upvotes: 0

Related Questions