Unable to use chromedriver on linux server [Exec format error] - python

I have a raspberry-pi running linux-server as platform. Therefore there is no GUI and I execute all my tasks through terminal by SSH-ing into the Pi. Platform details:
uname -a
>> Linux ubuntu 5.4.0-1080-raspi #91-Ubuntu SMP PREEMPT Thu Jan 19 09:35:03 UTC 2023 aarch64 aarch64 aarch64 GNU/Linux
Chromium [No issues here]
I have installed Chromium through snap.
chromium --version
>> Chromium 109.0.5414.119 snap
I am able to run chromium, navigate to a website, and take a snapshot
chromium --headless --disable-gpu --screenshot https://www.wikipedia.com
>> 0215/140750.965255:WARNING:bluez_dbus_manager.cc(247)] Floss manager not present, cannot set Floss enable/disable.
[0215/140752.998408:WARNING:sandbox_linux.cc(385)] InitializeSandbox() called with multiple threads in process gpu-process.
[0215/140802.665622:INFO:headless_shell.cc(223)] 84646 bytes written to file screenshot.png
Chromedriver [Issues]
I downloaded chromedriver this way
wget https://chromedriver.storage.googleapis.com/2.37/chromedriver_linux64.zip
And moved Chromedriver to the applications folder after unzipping
I get this error when trying to get chromedriver version, let alone run it
chromedriver --version
>> bash: /usr/local/bin/chromedriver: cannot execute binary file: Exec format error
My Python Script [Issues]
Here is the script I want to be able to run finally
import selenium
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("--disable-gpu")
driver = webdriver.Chrome(options=options)
driver.get("https://www.wikipedia.com")
driver.save_screenshot("proof.png")
This is the error I get when I try to run it
python3 test.py
>> OSError: [Errno 8] Exec format error: 'chromedriver'
What I've Tried already
Using chromedriver directly through ChromeDriverManager
import selenium
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("--disable-gpu")
driver = webdriver.Chrome(service=Service(ChromeDriverManager(path=".", chrome_type=ChromeType.CHROMIUM).install()), options=options)
driver.get("https://www.wikipedia.com")
driver.save_screenshot("proof.png")
The error
OSError: [Errno 8] Exec format error: './.wdm/drivers/chromedriver/linux64/109.0.5414/chromedriver'
Checking file permissions
Made sure file has execute permissions
ls -l /usr/local/bin/chromedriver
>> -rwxr-xr-x 1 ubuntu ubuntu 20427216 Sep 8 2021 /usr/local/bin/chromedriver

As you are having a raspberry-pi running linux-server as platform with Chromium 109.0.5414.119 you need to use ChromeDriver of:
Version 109.0
Linux compatible format i.e. chromedriver_linux64.tar.gz
Your effective line of code will be:
wget https://chromedriver.storage.googleapis.com/109.0.5414.74/chromedriver_linux64.tar.gz

Related

How to resolve 'Process unexpectedly closed with status 11' for Selenium in IPython?

I run the following code in IPython (from official Python extension VSCode):
# %%
from selenium import webdriver
driver = webdriver.Firefox(
executable_path='.driver/geckodriver')
It gives me the following errors:
For GeckoDriver 0.28.0
WebDriverException: Message: Process unexpectedly closed with status 11
For GeckkoDriver 0.27.0:
WebDriverException: Message: invalid argument: can't kill an exited process
My setup:
Firefox: 82.0
GeckoDriver: 0.28.0
Selenium: 3.141.0
IPython: 7.19.0
Ubuntu: 20.10
Python3 venv
Note: This script works in terminal but not IPython
Thank you.
I had the same issue with Firefox driver and I change it to Chrome driver it works well now
# install chromium, its driver, and selenium
!apt update
!apt install chromium-chromedriver
!pip install selenium
# set options to be headless, ..
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
# open it, go to a website, and get results
wd = webdriver.Chrome(options=options)
wd.get("https://www.website.com")
print(wd.page_source) # results
According to your description, I installed the same configuration as yours on my computer. When I run the code in the .py file and .ipynb file in VSCode, the result can be displayed:
It is recommended that you check the following aspects:
Make sure that the kernel "ipykernel" required by Jupyter has been installed in your currently selected VSCode environment.
Please check the execution path of "geckodriver" in the code.
Please try to reload VSCode or restart VSCode or even your computer if conditions permit.

Include Selenium/Firefox in Azure Function without Docker [duplicate]

Server: Raspberry Pi 3
OS: Dietpi - version 159
Geckodriver version: 0.22 for arm
Firefox version: 52.9.0
Python version: 3.5
Selenium version: 3.14.1
Gecko is executable, and is located in /usr/local/bin/
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.options import Options
import time
options = Options()
options.set_headless(headless=True)
driver = webdriver.Firefox(firefox_options=options)
print('Need your login credential')
username = input('What is your username?:\n')
password = input('What is your password?:\n')
...
...
Output:
root#RPi3:~# python3.5 ITE-bot.py
Traceback (most recent call last):
File "ITE-bot.py", line 12, in <module>
driver = webdriver.Firefox(firefox_options=options)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
keep_alive=True)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: invalid argument: can't kill an exited process
Any idea what is wrong? I've tried google without luck.
If you are running Firefox on a system with no display, make sure you use headless mode.
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
Also, make sure you have compatible versions of Firefox, Selenium, and Geckodriver:
https://firefox-source-docs.mozilla.org/testing/geckodriver/Support.html
Thumb rule
A common cause for Browsers to crash during startup is running WebDriver initiated Browsers 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 Browser as a regular user instead.
This error message...
selenium.common.exceptions.WebDriverException: Message: invalid argument: can't kill an exited process
...implies that the GeckoDriver was unable to initiate/spawn a new WebBrowsing Session i.e. Firefox Browser session.
Your main issue is the incompatibility between the version of the binaries you are using as follows:
Your GeckoDriver version is 0.22.0.
Release Notes of GeckoDriver v0.21.0 (2018-06-15) clearly mentions the following:
Firefox 57 (and greater)
Selenium 3.11 (and greater)
Your Firefox version is 52.9.0.
So there is a clear mismatch between GeckoDriver v0.22.0 and the Firefox Browser v57
Solution
Upgrade GeckoDriver to GeckoDriver v0.22.0 level.
GeckoDriver is present in the specified location.
GeckoDriver is having executable permission for non-root users.
Upgrade Firefox version to Firefox v62.0.2 levels.
Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
Execute your Selenium Test as a non-root user.
GeckoDriver, Selenium and Firefox Browser compatibility chart
I was on headless mode, using correct versions of everything, and the only way to get out of this error message was not to execute the selenium test as root
Yes checked Start Xvfb before the build can fix the problem, but if you have a job like a pipeline or multibranch pipeline this option is not visible. In the node of your Selenium grid that you go to execute the test you need:
1- Install Xvfb: apt install xvfb
2- Execute Xvfb: /usr/bin/Xvfb :99 -ac -screen 0 1024x768x8 & export DISPLAY=":99"
3- Rerun your node, for example: java -jar selenium.jar -role node -hub http://#.#.#.#:4444/grid/register -capabilities browserName=firefox,plataform=linux -host #.#.#.# -port 1991
This solution worked for me
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
As there can be many different underlying causes for this error it is best to find the root cause setting selenium to use debug level logging. In my case, for Ruby with capybara I needed to set: Selenium::WebDriver.logger.level = :debug. And voilĂ , running the same spec I could see in the logs that a dependency was missing, in my case:
libdbus-glib-1.so.2: cannot open shared object file: No such file or directory
Couldn't load XPCOM.
After installing it all worked fine.
I used:
VS Code
Linunx/Ubuntu:18.10
Nightwatch.js
My problem was that I tried to run Nightwatch (which automatically starts GeckoDriver) from the VS Code terminal.
I had the same problem, and realized that the real problem was some firefox dependencies not being installed inside the docker container I was testing in.
Try to initiate firefox and check if it returns an error.
As Nico and jay have stated you need to check the logs to see the details of the error. As you might use different systems, you can specify the path where the log is stored (i.e. "/tmp/geckodriver.log").
from selenium import webdriver
firefox_options = webdriver.firefox.webdriver.Options()
driver = webdriver.Firefox(log_path="/tmp/geckodriver.log",
options=firefox_options)
In my particular case, what the log said was:
Error: no DISPLAY environment variable specified
That was resolved adding in the options the headless mode before starting the driver. With the line:
firefox_options.set_headless()
I was able to fix this by running my tests with Xvfb. I was running them on a remote server.
I was using Jenkins so I checked the box that looked like this:
Credit to https://www.obeythetestinggoat.com/book/chapter_CI.html
in my case, I was running test cases as root
geckodriver.log
1576076416677 mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofilenCbl2e"
Running Firefox as root in a regular user's session is not supported. ($HOME is /home/seluser which is owned by seluser.)
1576077143004 mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofile7wpSQ7"
1576077143689 addons.webextension.screenshots#mozilla.org WARN Loading extension 'screenshots#mozilla.org': Reading manifest: Invalid extension permission: mozillaAddons
1576077143689 addons.webextension.screenshots#mozilla.org WARN Loading extension 'screenshots#mozilla.org': Reading manifest: Invalid extension permission: telemetry
1576077143689 addons.webextension.screenshots#mozilla.org WARN Loading extension 'screenshots#mozilla.org': Reading manifest: Invalid extension permission: resource://pdf.js/
1576077143689 addons.webextension.screenshots#mozilla.org WARN Loading extension 'screenshots#mozilla.org': Reading manifest: Invalid extension permission: about:reader*
1576077145372 Marionette INFO Listening on port 35571
1576077145423 Marionette WARN TLS certificate errors will be ignored for this session
1576077200207 mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofilenhoHlr"
Running Firefox as root in a regular user's session is not supported. ($HOME is /home/seluser which is owned by seluser.)
i could get around by
cd /home
chown -R root seluser
i woundnt say its correct but it got my job done

How to properly use selenium with geckodriver and firefox with python on Ubuntu?

I am trying to use the geckodriver with firefox and selenium on my Ubuntu machine. This is the code I have so far:
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
#path where browser is installed
binary = '/usr/bin/firefox'
options = webdriver.FirefoxOptions()
options.binary = binary
options.add_argument('start-maximized')
options.add_argument('--headless')
cap = DesiredCapabilities().FIREFOX
cap["marionette"] = False
path_to_driver = "/home/andrea/geckodriver"
# run firefox webdriver from executable path
driver = webdriver.Firefox(firefox_options=options, capabilities=cap, executable_path = path_to_driver)
#driver = webdriver.Firefox(capabilities=cap, executable_path = path_to_driver)
driver.get("https://www.amboss.com/us/account/login")
Despite that I am getting the following error:
selenium.common.exceptions.WebDriverException: Message: Can't load the profile.
Possible firefox version mismatch. You must use GeckoDriver instead for Firefox 48+. Profile Dir: /tmp/tmpuigrk9f7 If you specified a log_file in the FirefoxBinary constructor, check it for details.
The firefox version which I work with is:
Mozilla Firefox 68.0.2
Does anyone have any idea as to how I could go about fixing this?
Step1: Install Selenium
Type in Terminal(in Ubuntu) or in Command Prompt(in Windows)
$pip install selenium
Step2: Download Geckodriver
In order to work with Selenium there should be an executable called 'Gecko Driver' installed.
Download Gecko Driver from the following page:
https://github.com/mozilla/geckodriver/releases
Step3: Install Gecko Driver
Latest version for Windows:
https://github.com/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-win64.zip
Latest version for Ubuntu:
https://github.com/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-linux64.tar.gz
Setup Gecko Driver For Windows:
Extract the zip file and move the geckodiver.exe executable file to any location which is already in Path variable(For Example you can move it to Python path location)
Unless add the path of 'geckodriver.exe' to the Path variable
Setup Gecko Driver For Ubuntu:
Open Terminal
Ctrl+Alt+T
move directory to the location where tar file is downloaded
Usually it will be in Downloads. so type $ cd Downloads
Unzip the tar file
eg:
$sudo tar -xvf filename.tar.gz
In my case it is:
$sudo tar -xvf geckodriver-v0.26.0-linux64.tar.gz
Move the geckodriver executable file to the '/usr/local/bin' location
$sudo mv geckodriver /usr/local/bin/
Move the directory to '/usr/local/bin/'
$cd /usr/local/bin/
Now make executable permission for 'geckodriver' executable file
$sudo chmod +x geckodriver
Now type 'geckodriver' in Terminal
geckodriver
If Gecko Driver is not working still then add its path
$export PATH=$PATH:/usr/local/bin/geckodriver
Now it is ready to work with selenium
Sample Code
Some sample codes are here:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import ui
driver = webdriver.Firefox()
driver.get('https://www.google.com/')
page_url=driver.find_elements_by_xpath("//a[#class='content']")
all_title = driver.find_elements_by_class_name("title")
title = [title.text for title in all_title]
print(title)
This error message...
selenium.common.exceptions.WebDriverException: Message: Can't load the profile.
Possible firefox version mismatch. You must use GeckoDriver instead for Firefox 48+. Profile Dir: /tmp/tmpuigrk9f7 If you specified a log_file in the FirefoxBinary constructor, check it for details.
...implies that there was a mismatch between the GeckoDriver and Firefox version while initiating/spawning a new WebBrowsing Session i.e. Firefox Browser session.
Your main issue is the incompatibility between the version of the binaries you are using as follows:
You are using Mozilla Firefox v68.0.2
Your Selenium Client version is is unknown to us.
Your GeckoDriver version is unknown to us.
However as you are using Mozilla Firefox v68.0.2, using GeckoDriver is mandatory and while you use GeckoDriver you can't set the capability marionette as False.
You can find a detailed discussion in How can Geckodriver/Firefox work without Marionette? (running python selenium 3 against FF 53)
Solution
Upgrade Selenium to current levels Version 3.141.59.
Upgrade GeckoDriver to current GeckoDriver v0.24.0 level.
GeckoDriver is present in the specified location.
GeckoDriver is having executable permission for non-root users.
Upgrade Firefox version to Firefox v68.0.2 levels.
Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
Take a System Reboot.
Execute your Test as a non-root user.
Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.
Outro
GeckoDriver, Selenium and Firefox Browser compatibility chart
If you want to use Firefox with Selenium, you need to import e Firefox Profile. You can use your own Profile through the following steps :
Locate the Firefox Profile directory
You have to specify the absolute path of the Firefox Profile directory when you initiate the webdriver.
from selenium import webdriver
profile = webdriver.FirefoxProfile(*path to your profile*)
driver = webdriver.Firefox(firefox_profile=profile)

Facing chromedriver unexpectedly exited. Status code was: 127 while using Headless chrome browser with docker & python 3.7 on AWS ec2

This is not a duplicate of other questions & I have tried the existing answers as well. On AWS ec2 I have created a docker container in which I am trying to open headless chrome. The docker file is
FROM python:3
# Install Chrome for Selenium
RUN curl https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb -o /chrome.deb
RUN dpkg -i /chrome.deb || apt-get install -yf
RUN rm /chrome.deb
RUN wget https://chromedriver.storage.googleapis.com/2.38/chromedriver_linux64.zip
RUN unzip chromedriver_linux64.zip -d chromedriver
RUN chmod +x /chromedriver/chromedriver
ENV PATH="/chromedriver:${PATH}"
RUN echo 'logging path'
RUN echo "${PATH}"
The code which I am using to open browser is:
options = Options()
options.add_argument("--window-size=1920,1080")
options.add_argument("--disable-gpu")
options.add_argument("--disable-extensions")
options.add_argument("--proxy-server='direct://'")
options.add_argument("--proxy-bypass-list=*")
options.add_argument("--start-maximized")
options.add_argument("--headless")
options.add_argument('--no-sandbox')
options.headless = True
driver = webdriver.Chrome(options=options, executable_path='/chromedriver/chromedriver')
driver.get(url)
The error which on ec2 is:
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
self.service.start()
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 98, in start
self.assert_process_still_running()
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 111, in assert_process_still_running
% (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service /chromedriver/chromedriver unexpectedly exited. Status code was: 127
I have tried Installing Chromium on Amazon Linux and other stackoverflow answers as well but unable to solve it.
Google-chrome version - Google Chrome 75.0.3770.100
driver version - 2.38
selenium - 3.141.0
The OS of AWS AMI is linux and not ubuntu thuscommand ldd chrome | grep not would help. Even though I have fixed that issue as well, still getting error. When logged the path of chromedriver it's logged as below
/usr/local/lib/python3.7/site-packages/chromedriver_binary:/chromedriver:
I had a similar error which was fixed by installing xvfb, this is required because you are running a headless session of chrome. Try doing apt-get install xvfb

Selenium fails to start Chromedriver

I am unable to start Chromedriver with Selenium.
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('http://www.google.com')
It never gets to browser.get('http://www.google.com') but fails with:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(Driver info: chromedriver=2.21.371461 (633e689b520b25f3e264a2ede6b74ccc23cb636a),platform=Linux 3.13.0-76-generic x86_64)
I am on Ubuntu 14.04 (64b) using Python 2.7.6 (virtualenv installation) and selenium==2.50.1.
dm#Z580:~$ which chromedriver
/usr/local/bin/chromedriver
dm#Z580:~$ ll /usr/local/bin/chromedriver
lrwxrwxrwx 1 root root 24 feb 4 22:13 /usr/local/bin/chromedriver -> /opt/google/chromedriver*
EDIT
dm#Z580:~$ google-chrome --version
Google Chrome 48.0.2564.97
Downgraded to Chromedriver 2.20:
wget http://chromedriver.storage.googleapis.com/2.20/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
chmod 777 chromedriver
sudo mv -f chromedriver /opt/google/chromedriver
but, sadly, still the same result.
Any idea why is this happening?
Ok, so the solution is a bit weird.
I moved the Chromedriver binary from /opt/google/ (where Google Chrome is installed itself) to /opt/, updated the symlink and it's working now!
Compared to the question itself, these two lines of code solve the issue:
sudo mv /opt/google/chromedriver /opt/
sudo ln -fs /opt/chromedriver /usr/local/bin/chromedriver
And now I am able to run the following Python code:
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('http://www.google.com')
Chrome starts and everything.
The following line on the old Selenium docs page inspired me to check and eventually to change the Chromedriver location:
For Linux systems, the ChromeDriver expects /usr/bin/google-chrome to
be a symlink to the actual Chrome binary.
I don't know if this is the only issue you're having but chromedriver doesn't like "www.google.com", however it is fine with "http://www.google.com"

Categories

Resources