Question

How do I set up Chromedriver path in droplets

I deployed a flask app with selenium and I installed chromedriver and move to /usr/bin/chromedriver in debian 12 x64 OS. But it’s still errored out. error message:

 Traceback (most recent call last):
[flight] [2023-11-27 02:54:29]   File "/workspace/.heroku/python/lib/python3.11/site-packages/gunicorn/arbiter.py", line 609, in spawn_worker
[flight] [2023-11-27 02:54:29]     worker.init_process()
[flight] [2023-11-27 02:54:29]   File "/workspace/.heroku/python/lib/python3.11/site-packages/gunicorn/workers/base.py", line 134, in init_process
[flight] [2023-11-27 02:54:29]     self.load_wsgi()
[flight] [2023-11-27 02:54:29]   File "/workspace/.heroku/python/lib/python3.11/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
[flight] [2023-11-27 02:54:29]     self.wsgi = self.app.wsgi()
[flight] [2023-11-27 02:54:29]                 ^^^^^^^^^^^^^^^
[flight] [2023-11-27 02:54:29]   File "/workspace/.heroku/python/lib/python3.11/site-packages/gunicorn/app/base.py", line 67, in wsgi
[flight] [2023-11-27 02:54:29]     self.callable = self.load()
[flight] [2023-11-27 02:54:29]                     ^^^^^^^^^^^
[flight] [2023-11-27 02:54:29]   File "/workspace/.heroku/python/lib/python3.11/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
[flight] [2023-11-27 02:54:29]     return self.load_wsgiapp()
[flight] [2023-11-27 02:54:29]            ^^^^^^^^^^^^^^^^^^^
[flight] [2023-11-27 02:54:29]   File "/workspace/.heroku/python/lib/python3.11/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
[flight] [2023-11-27 02:54:29]     return util.import_app(self.app_uri)
[flight] [2023-11-27 02:54:29]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[flight] [2023-11-27 02:54:29]   File "/workspace/.heroku/python/lib/python3.11/site-packages/gunicorn/util.py", line 371, in import_app
[flight] [2023-11-27 02:54:29]     mod = importlib.import_module(module)
[flight] [2023-11-27 02:54:29]           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[flight] [2023-11-27 02:54:29]   File "/workspace/.heroku/python/lib/python3.11/importlib/__init__.py", line 126, in import_module
[flight] [2023-11-27 02:54:29]     return _bootstrap._gcd_import(name[level:], package, level)
[flight] [2023-11-27 02:54:29]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[flight] [2023-11-27 02:54:29]   File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
[flight] [2023-11-27 02:54:29]   File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
[flight] [2023-11-27 02:54:29]   File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
[flight] [2023-11-27 02:54:29]   File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
[flight] [2023-11-27 02:54:29]   File "<frozen importlib._bootstrap_external>", line 940, in exec_module
[flight] [2023-11-27 02:54:29]   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
[flight] [2023-11-27 02:54:29]   File "/workspace/app.py", line 70, in <module>
[flight] [2023-11-27 02:54:29]     driver = webdriver.Chrome(options=option)
[flight] [2023-11-27 02:54:29]              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[flight] [2023-11-27 02:54:29]   File "/workspace/.heroku/python/lib/python3.11/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
[flight] [2023-11-27 02:54:29]     super().__init__(
[flight] [2023-11-27 02:54:29]   File "/workspace/.heroku/python/lib/python3.11/site-packages/selenium/webdriver/chromium/webdriver.py", line 53, in __init__
[flight] [2023-11-27 02:54:29]     self.service.start()
[flight] [2023-11-27 02:54:29]   File "/workspace/.heroku/python/lib/python3.11/site-packages/selenium/webdriver/common/service.py", line 102, in start
[flight] [2023-11-27 02:54:29]     self.assert_process_still_running()
[flight] [2023-11-27 02:54:29]   File "/workspace/.heroku/python/lib/python3.11/site-packages/selenium/webdriver/common/service.py", line 115, in assert_process_still_running
[flight] [2023-11-27 02:54:29]     raise WebDriverException(f"Service {self._path} unexpectedly exited. Status code was: {return_code}")
[flight] [2023-11-27 02:54:29] selenium.common.exceptions.WebDriverException: Message: Service /workspace/.cache/selenium/chromedriver/linux64/119.0.6045.105/chromedriver unexpectedly exited. Status code was: 127

code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
#service = Service(executable_path='chromedriver.exe')
from selenium.webdriver.chrome.options import Options
option = Options()

#option = webdriver.ChromeOptions()
option.add_argument("--headless=new")
#option.add_argument('--ignore-certificate-errors')
#option.add_argument("--test-type")
#options.binary_location = "/usr/bin/chromium"
#option.add_argument("--no-sandbox")
#option.add_argument('disable-notifications')
#driver = webdriver.Chrome(service=service,options=option)

driver = webdriver.Chrome(options=option)

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
November 27, 2023

Heya,

The error message you’re encountering, selenium.common.exceptions.WebDriverException: Message: Service /workspace/.cache/selenium/chromedriver/linux64/119.0.6045.105/chromedriver unexpectedly exited. Status code was: 127, suggests a few potential issues with your Selenium setup in your Flask app deployed on a Debian 12 x64 OS. Here are some steps to troubleshoot and potentially resolve the issue:

  1. Chromedriver Compatibility: Ensure that the version of chromedriver you’re using is compatible with the version of Chrome installed on your system. The error might be due to a version mismatch.

  2. Chromedriver Path: The error indicates that Selenium is trying to use a chromedriver from a cached path (/workspace/.cache/selenium/chromedriver/linux64/119.0.6045.105/chromedriver). You need to specify the path to the chromedriver that you moved to /usr/bin/. You can do this by uncommenting and modifying the Service line in your code:

service = Service(executable_path='/usr/bin/chromedriver')
driver = webdriver.Chrome(service=service, options=option)
  1. Headless Chrome Options: Your current options setup seems to be commented out. Ensure that you have the necessary options enabled for running Chrome in a headless environment, especially if you are running this in a server environment without a GUI:
option.add_argument("--headless")
option.add_argument("--no-sandbox") 
option.add_argument("--disable-dev-shm-usage")
  1. Chrome Binary Location: If Chrome is not installed in the default location, you might need to specify its binary location in the options:
option.binary_location = "/path/to/google-chrome"
  1. Permissions and Environment: Ensure that the environment where the Flask app is running has the necessary permissions to execute chromedriver and access Google Chrome.

  2. Dependencies and Libraries: Make sure that all dependencies and libraries are correctly installed and compatible with each other in your deployment environment.

After making these adjustments, try running your Flask application again. If the problem persists, you may need to look into more detailed logs or error messages for further clues.

Bobby Iliev
Site Moderator
Site Moderator badge
November 27, 2023

Hi there,

I noticed in the output you shared that this might be deployed on the DigitalOcean App Platform. Could you confirm if this is the case?

If your application is indeed running on the App Platform, you would need to manage custom dependencies, like ChromeDriver, differently than on a traditional Droplet. The App Platform uses buildpacks to create your application’s environment, which may not include all the dependencies required for Selenium and ChromeDriver to run properly.

To install custom dependencies, you can define your own environment using a Dockerfile. This allows you to specify exactly what software and versions you need, including Chrome and ChromeDriver. You would then deploy your app using this Dockerfile, which the App Platform will use to build a container for your application:

App Platform Buildpacks

If this is not the case and you are indeed deploying to a Dropler, setting up the ChromeDriver path in a Debian Droplet for a Selenium-powered Flask application involves a few steps to ensure that the driver can be found and executed properly by your application. The error message you’re seeing suggests that the ChromeDriver is not being executed correctly, possibly because of an incorrect path or missing dependencies.

  1. Install ChromeDriver:

    • Ensure you download the correct version of ChromeDriver that matches the version of Chrome installed on your Droplet.
    • You can install ChromeDriver using a package manager like apt or manually downloading it from the ChromeDriver website.
  2. Set the Correct Path:

    • When you move chromedriver to /usr/bin/, it should be executable by just calling chromedriver from anywhere in your system.
    • Verify that the executable is indeed in /usr/bin/ by running which chromedriver. The output should be /usr/bin/chromedriver.
  3. Set Permissions:

    • Ensure that the chromedriver has the correct permissions set. It should be executable. You can set the permissions using:

      sudo chmod +x /usr/bin/chromedriver
      
  4. Update Your Selenium Code:

    • In your Python code, you can explicitly set the path to chromedriver using the Service object from Selenium. However, if you have placed chromedriver in /usr/bin/, this is not necessary as the system should be able to locate it automatically.
    • Make sure the options you’re adding to Chrome are correct. For instance, --headless=new is not a valid argument. The correct argument for running Chrome in headless mode is --headless.

Here’s an example of how your updated Python code might look:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

option = Options()
option.add_argument("--headless")  # Correct argument for headless mode

# Ensure Chrome is installed and update the binary_location if necessary
# option.binary_location = "/usr/bin/google-chrome"

driver = webdriver.Chrome(options=option)

# Your code to use `driver` goes here
  1. Install Chrome:

    • If you haven’t installed Google Chrome, you’ll need to do so as ChromeDriver requires it. You can typically install Google Chrome on Debian with the following commands:

      wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
      sudo apt install ./google-chrome-stable_current_amd64.deb
      
  2. Check for Missing Dependencies:

    • If ChromeDriver is exiting unexpectedly, it may be due to missing libraries. Run ChromeDriver directly from the command line (/usr/bin/chromedriver) to see if there are any error messages that indicate missing dependencies.
  3. Logging:

    • To get more detailed logs from ChromeDriver, you can pass the service_log_path argument to the Service object in your Selenium code. This can help identify why ChromeDriver is exiting unexpectedly.

Let me know how it goes!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel