Selenium firefox webdriver with virtualenv: Exec format error - selenium

I am using Selenium and Virtualenv in Ubuntu14.04 to scrape some code on webpage. After making a virtualenv directory, while activating virtualenv using
source bin/activate,
I installed selenium, headless firefox, pyvirtualdisplay and firefox geckodriver.
I installed headless firefox according to here.
And I also Found out that there are different python2.7 folders at
"/usr/lib/python2.7/" and "~/ladder_selenium/lib/python2.7" (ladder_selenium is the Virtualenv directory)
and after I ran a python code which contains
from selenium import webdriver
, the error below happens.
Traceback (most recent call last):
File "predict.py", line 12, in <module>
driver = webdriver.Firefox()
File "/home/heyjude/ladder_selenium/local/lib/python2.7/site-packages/selenium/webdriver/f
self.service.start()
File "/home/heyjude/ladder_selenium/local/lib/python2.7/site-packages/selenium/webdriver/c
stdout=self.log_file, stderr=self.log_file)
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 8] Exec format error
Could you tell me what to fix?
EDIT for the COMMENT BELOW:
(ladder_selenium) heyjude#ladder-selenium:~$ export DISPLAY=:99
(ladder_selenium) heyjude#ladder-selenium:~$ firefox
XPCOMGlueLoad error for file /opt/firefox/libmozgtk.so:
libgtk-3.so.0: cannot open shared object file: No such file or directory
Couldn't load XPCOM.

Related

Selenium 4 and webdriver manager for firefox fails to run in Ubuntu

Apologies I am quite new to all this so trying to figure out if I am doing the right thing. I am running Ubuntu 22.04 and Selenium 4.8
I installed latest Geckodriver from Mozilla GitHub, converted it into executable and then moved it to /usr/local/bin/
I then downloaded webdriver manager and tried to run the following snippet for Firefox:
# selenium 4 - updating geckodriver for firefox
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
When I run that, it starts downloading: [WDM] - Downloading: 19.0kB [00:00, 13.9MB/s] and then it fails I get following error: selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 127
full error:
[WDM] - Downloading: 19.0kB [00:00, 13.9MB/s]
Traceback (most recent call last):
File "/home/gizmo/PycharmProject/PythonProjects/web/firefox_selenium.py", line 6, in <module>
driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()))
File "/home/gizmo/PycharmProject/PythonProjects/venv/lib/python3.10/site-packages/selenium/webdriver/firefox/webdriver.py", line 196, in __init__
super().__init__(command_executor=executor, options=options, keep_alive=True)
File "/home/gizmo/PycharmProject/PythonProjects/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 286, in __init__
self.start_session(capabilities, browser_profile)
File "/home/gizmo/PycharmProject/PythonProjects/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 378, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/gizmo/PycharmProject/PythonProjects/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "/home/gizmo/PycharmProject/PythonProjects/venv/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 127
Where am I going wrong?
Goal: tried to get the webdriver manager to update my geckodriver for Firefox - ran it as a test to see if it is all setup correctly.
Pre-requisites
Ensure that:
Selenium is upgraded to v4.0.0 or above
pip3 install -U selenium
Webdriver Manager for Python is installed
pip3 install webdriver-manager
You can find a detailed discussion on installing Webdriver Manager for Python in ModuleNotFoundError: No module named 'webdriver_manager' error even after installing webdrivermanager
You don't have to cast to FirefoxService instance and can use the Service instance. You can use:
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()))
Note: Ensure that your firefox is updated to the latest version.

OSError: [Errno 8] Exec format error: '/home/ec2-user/Desktop/chromedriver' error using Chromedriver in AWS EC2 ARM flavour machine

I have an EC2 machine from AWS with ARM flavour. I installed Python 3, and then used pip3 to install Selenium. I then downloaded the Linux version of chrome driver from here, unzipped it and kept the chromedriver file in Desktop. When I try to run code like this:
driver = webdriver.Chrome('/home/ec2-user/Desktop/chromedriver')
I get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ec2-user/.local/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
service_log_path, service, keep_alive)
File "/home/ec2-user/.local/lib/python3.7/site-packages/selenium/webdriver/chromium/webdriver.py", line 90, in __init__
self.service.start()
File "/home/ec2-user/.local/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 76, in start
creationflags=self.creationflags)
File "/usr/lib64/python3.7/subprocess.py", line 800, in __init__
restore_signals, start_new_session)
File "/usr/lib64/python3.7/subprocess.py", line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: '/home/ec2-user/Desktop/chromedriver'
Any idea how to fix this? May be a different combination of Linux flavour and chrome driver from somewhere else?
This error message...
OSError: [Errno 8] Exec format error: '/home/ec2-user/Desktop/chromedriver'
...implies that you are using an incorrect format of ChromeDriver executable.
Deep Dive
As you are using AWS EC2 ARM flavour you need to use the either ARM64 or ARM7 variant of ChromeDriver.
Additionally, you seem to have downloaded ChromeDriver v100.x where as the latest version of google-chrome is Chrome Version 99.0.4844.84
and you need the matching version of ChromeDriver as per the installed version of Google Chrome within the system.
Solution
As #Mark B pointed out in his comment you need to download the matching version of ChromeDriver as per the discussion Compile ChromeDriver on ARM

How do I add chromedriver to PATH?

I'm trying to follow Automate the Boring Stuff and am trying to do the selenium work. I’m running Linux using Mu. Here's the code I'm trying to run
from selenium import webdriver
chromedriver = “/lorenmspeer/Downloads/chromedriver.exe”
browser = webdriver.Chrome(executable_path=chromedriver)
url = “https://www.duckduckgo.com”
browser.get(url)
I’m getting this error:
Traceback (most recent call last):
File “/home/lorenmspeer/.local/share/mu/mu_venv-38-20210401-164244/lib/python3.8/site-packages/selenium/webdriver/common/service.py”, line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File “/usr/lib/python3.8/subprocess.py”, line 854, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File “/usr/lib/python3.8/subprocess.py”, line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: ‘/lorenmspeer/Downloads/chromedriver.exe’
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/home/lorenmspeer/mu_code/seleniumTest22.py”, line 5, in
browser = webdriver.Chrome(executable_path=chromedriver)
File “/home/lorenmspeer/.local/share/mu/mu_venv-38-20210401-164244/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py”, line 73, in init
self.service.start()
File “/home/lorenmspeer/.local/share/mu/mu_venv-38-20210401-164244/lib/python3.8/site-packages/selenium/webdriver/common/service.py”, line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver.exe’ executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
To check where Chrome driver is installed on Linux run whereis chromedriver. Then, put this path to Chrome("your_path")
In my project it is specified in the folder I defined:
driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver")
It should not have .exe resolution
For more info check docs:
https://chromedriver.chromium.org/getting-started
driver = webdriver.Chrome('/path/to/chromedriver') # Optional argument, if not specified will search path.
I found a solution. First I had to make the file executable:
chmod +x chromedriver
Then I moved it to PATH
sudo mv chromedriver /usr/local/bin/
This made the code launch the browser.

Selenium ERROR in Run the simple program in Linux

I'm new in python and Linux also. I try to install selenium in python 3.7
but i got the error:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.ubuntu.com/')
output
Traceback (most recent call last):
File "test.py", line 1, in <module>
from selenium import webdriver
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/__init__.py", line 18, in <module>
from .firefox.webdriver import WebDriver as Firefox # noqa
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 29, in <module>
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 26, in <module>
from .webelement import WebElement
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webelement.py", line 37, in <module>
from StringIO import StringIO as IOStream File
"/usr/lib/python3.7/StringIO.py", line 40
raise ValueError, "I/O operation on closed file"
^ SyntaxError: invalid syntax
you need to downgrade to python 3.6
Or if you do not have gecko driver go through following steps.
wget https://github.com/mozilla/geckodriver/releases/download/v0.23.0/geckodriver-v0.23.0-linux64.tar.gz
sudo sh -c 'tar -x geckodriver -zf geckodriver-v0.23.0-linux64.tar.gz -O > /usr/bin/geckodriver'
sudo chmod +x /usr/bin/geckodriver
rm geckodriver-v0.23.0-linux64.tar.gz

python - control the browser with selenium module

I installed python version 3.5.2, selenium version 3.0.1 and firefox version 49.0.2.
After all of that and in my shell scripting I typed
>>> from selenium import webdriver
>>> browser = webdriver.Firefox()
and got error as this below:
Could someone guide me on what is wrong here.
I appreciate it. I am just starting out on python. Thanks
#
Traceback (most recent call last):
File "C:\Users\D\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 64, in start
stdout=self.log_file, stderr=self.log_file)
File "C:\Users\D\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "C:\Users\D\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 1224, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
browser = webdriver.Firefox()
File "C:\Users\D\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 135, in __init__
self.service.start()
File "C:\Users\D\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
#