Jengi829
Jengi829

Reputation: 1

DevToolsActivePort file doesn't exist" When Running Flask App with Selenium via Gunicorn Service

I'm trying to run a Flask app that uses Selenium with Chrome WebDriver on Ubuntu, but I encounter the following error when I run the app using gunicorn as a service:

`(nknown 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.)

Here’s a summary of my setup:

Python Code: This is the relevant part of my Flask app code:

from flask import Flask, request, jsonify
from datetime import datetime
import json
import sys
import requests
import sys
import logging
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


# Configure the logging settings
logging.basicConfig(
    level=logging.INFO,  # Capture INFO and above (INFO, WARNING, ERROR, CRITICAL)
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(),  # Prints to console
        logging.FileHandler('app.log', mode='a')  # Writes to a file named app.log
    ]
)

logging.info("Initializing the Chrome browser...")  # Debug message

# Set up Chrome browser options
options = webdriver.ChromeOptions()
options.add_argument("--disable-gpu"

)
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
# Uncomment for debugging (visible browser)
options.add_argument("--headless")
logging.info("Settings setup...")
# Initialize the WebDriver
try:
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
    
    logging.info("Chrome WebDriver initialized successfully.")
except Exception as e:
    logging.info(f"Error initializing Chrome WebDriver: {e}")


app = Flask(__name__)
logging.info("This is a test.")

This is the .service file I'm using for gunicorn:


[Unit]
Description=Gunicorn instance to serve test Flask app
After=network.target

[Service]
User=shane
Group=www-data
WorkingDirectory=/home/shane/miniconda3/envs/test
Environment="PATH=/home/shane/miniconda3/envs/test/bin:/home/shane/miniconda3/condabin:$PATH"
ExecStart=/usr/bin/gunicorn --bind unix:app.sock wsgi:app

[Install]
WantedBy=multi-user.target

I start the service by running this command: sudo systemctl restart test.service

WSGI File:


from app import app
if __name__ == '__main__':
    app.run()

Error Details in app.log:

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

How can I fix this issue?

Verified that the chromedriver and google-chrome installations are correct. When I simply run the python code, I get no error. It is when I run with Gunicorn I get this error.

Upvotes: 0

Views: 45

Answers (0)

Related Questions