2

I have a selenium script that I execute in another python program. This program will only execute when I am logged into the server using ssh as root but not executable by the www-data user because it returns with the error:

selenium.common.exceptions.WebDriverException: Message: Service /usr/bin/chromedriver unexpectedly exited. Status code was: 1

I run the script using this command:

os.system('python3 /var/website/webscraping.py' + str(VARIABLE))

Any help would be appriciated!

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Python 123
  • 59
  • 1
  • 13
  • `www-data` is a user that has low privileges intentionally, for security reasons. You should find a different approach than running executables from your webserver. – Klaus D. Dec 31 '21 at 02:40
  • I am running the selenium script as a python file but it does not run as www-data user. – Python 123 Dec 31 '21 at 03:21
  • 1
    Have you tried using `chmod +x` for the `www-data` user on the files you are working with? – S P Sharan Jan 04 '22 at 10:49
  • The user www-data has the proper permissions to read, write, and execute the chromedriver, google-chromium, and the python script selenium is being run in – Python 123 Jan 05 '22 at 04:32
  • @Python123 Can you try running the driver with the www-data user without the python side of things? `/usr/bin/chromedriver --version` and see if it errors out? – testfile Jan 06 '22 at 15:13
  • I got this output Sorry, home directories outside of /home are not currently supported. See https://forum.snapcraft.io/t/11209 for details. – Python 123 Jan 07 '22 at 02:37

2 Answers2

1

Ideally you should have been able to execute the program as www-data user. However this error message...

selenium.common.exceptions.WebDriverException: Message: Service /usr/bin/chromedriver unexpectedly exited. Status code was: 1

...implies that the ChromeDriver unexpectedly exited.


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.


There can be numerous reasons behind this error. Your code trials and the complete error stacktrace would have given us some more visibility on what's wrong happening under the hood.

However, a couple of remedial steps are as follows:

  • Ensure that Chrome is updated to current chrome=96.0.4664.45 (as per chrome=96.0.4664.45 release notes).

  • Ensure that ChromeDriver is updated to current ChromeDriver v96.0 level.

  • Ensure that you have downloaded the exact format of the ChromeDriver binary from the download location pertaining to your underlying OS among:

    • chromedriver_win32: For Windows OS
    • chromedriver_mac64: For MAC OS X
    • chromedriver_linux64: For Linux OS
  • Using Selenium you need to pass the absolute path of the ChromeDriver binary through the argument executable_path and you need to mention the path within single quotes (i.e. '') seperated by a single forward slash (i.e. \) along with the raw switch (i.e. r) as follows:

    from selenium import webdriver
    
    driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver')
    driver.get(url)
    
  • Ensure that ChromeDriver binary have executable permission for the non-administrator user.

  • Execute your Test as a non-administrator user.

  • Another potential reason for the error can be due to missing the entry 127.0.0.1 localhost in /etc/hosts

    • Windows OS - Add 127.0.0.1 localhost to /etc/hosts

    • Mac OSX - Ensure the following entries:

      127.0.0.1   localhost
      255.255.255.255 broadcasthost
      ::1 localhost
      fe80::1%lo0 localhost   
      

References

As per the discussion in selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service geckodriver:

  • Selenium does not require 127.0.0.1 localhost to be explicitly set in the host file.
  • However it is mandatory requirement to map localhost to the IPv4 local loopback (127.0.0.1)
  • The mechanism of this mapping does not have to be through the hosts file always.
  • On Windows OS systems it is not mapped in the hosts file at all (resolving localhost is done by the DNS resolver).

TL;DR

How to reset the Hosts file back to the default


Update

As per your comment update:

chromedriver is at version 97 and google-chrome is at version 96

Your main issue seems to be the incompatibility between the version of the binaries you are using a there is a clear mismatch between chromedriver=97.0 and the chrome=96.0.4664.45

Solution

Ensure that:

You can find a relevant detailed discussion in WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally." (Driver info: chromedriver=97) using Selenium Python

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for the suggestions, chromedriver is at version 97 and google-chrome is at version 96. /etc/hosts has 127.0.0.1 localhost as the first entry in the file. I pass the executable path through already in my code. – Python 123 Jan 09 '22 at 18:14
  • @Python123 Checkout the updated answer and let me know the status. – undetected Selenium Jan 09 '22 at 18:30
  • Still I don't think the error is caused by the software version because the program runs normally as root user. – Python 123 Jan 09 '22 at 18:44
  • Also how would I downgrade the software? – Python 123 Jan 09 '22 at 18:47
  • @Python123 _chrome=96.0.4664.45_ seems perfect, you need to download the matching _chrome=96.0.4664.45_ from the embedded link within my answer and use the absolute location of it within your code. – undetected Selenium Jan 09 '22 at 18:54
  • 1
    THANK YOU!!!! This issue has been affecting me for a while! This solved my issue! – Python 123 Jan 09 '22 at 19:53
  • So sorry something happened and my server got deleted. How can I install the chrome browser to be at the version of the chromedriver? – Python 123 Jan 16 '22 at 05:08
-1

Looks like your browser version is not matching with the browser binary provided by you in your path. You can implement WebDriverManager to resolve your issue.

  1. Install WebDriverManager module for python

    pip install webdriver-manager

  2. Setup/initialize ChromeDriver service as below

//selenium 3
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

//selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

More information below -> https://cpsat.agiletestingalliance.org/2021/03/25/selenium-webdriver-manager-in-python/