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