Format error in python Selenium package - python

I'm running on Linux Debian 9 and PyCharmwith web scraping purposes; I'm currently using Python 3.5 as interpreter.
The script is the following:
from selenium import webdriver
import time
import datetime
from selenium.webdriver.common.keys import Keys
Till here, the script works fine, by importing the packages properly; when I try to set the driver by running the following line:
driver = webdriver.Firefox(executable_path='/home/quant/Desktop/DataDownload/venv/bin/geckodriver')
I get the following error message, relative to a format problem:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/home/quant/Desktop/DataDownload/venv/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 157, in __init__
self.service.start()
File "/home/quant/Desktop/DataDownload/venv/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 76, in start
stdin=PIPE)
File "/usr/lib/python3.5/subprocess.py", line 676, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.5/subprocess.py", line 1282, in _execute_child
raise child_exception_type(errno_num, err_msg)
OSError: [Errno 8] Exec format error
Browsing on the web, I found that probably this problem comes out when the executable program has not been unzipped and made executable correctly; to do that, I followed the steps below, by running them on the terminal:
(1).zip file download from the official repository in the github website:
wget [here][1]
(2) unzipped the file:
cd /home/quant/Downloads
tar -xvzf geckodriver-v0.21.0-arm7hf.tar.gz
(3) made the file executable:
chmod +x geckodriver
(4) moved the file on the following path:
mv geckodriver /home/quant/PycharmProject/DataDownloads/venv/bin/
Could someone help me to understand what's wrong, please?
Thanks in advance all!!

[Errno 8] Exec format error
This means you are trying to run a version of geckodriver that is compiled for a different architecture... you downloaded the ARM version (geckodriver-v0.21.0-arm7hf.tar.gz) and are most likely running on a x86/amd64 machine.
Solution:
go back to the geckodriver releases page and download the correct version for your system: https://github.com/mozilla/geckodriver/releases.
For example, if you are running 64 bit Linux, you want to download: geckodriver-v0.21.0-linux64.tar.gz

Related

Mac can't find chromedriver when running python script from launchctl

I've been trying to schedule one of my python scripts through launchctl and am coming up against an error. The intention is that this runs at a given time past the hour, every hour.
I've created my plist, loaded it in launchctl but it's not working properly. After doing some digging I debugged and got the following error messages:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 854, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Library/Frameworks/Python.framework/Versions/3.8/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: 'chromedriver'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/petersmith/Documents/Python/thenewsproject/link_getters/link_getter.py", line 3, in
import myfile
File "/Users/directories/myfile.py", line 51, in
browser = webdriver.Chrome('chromedriver', options=chrome_options)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in init
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
I've updated the items in bold when pasting.
This is the line that it's fussing about:
browser = webdriver.Chrome('chromedriver', options=chrome_options)
Now, chromedriver lives in my usr/local/bin folder and I've checked my path file and see:
-bash: /Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/mysql/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Applications/Xamarin: No such file or directory
You can see in bold that it's there so I'm a little lost.
Any ideas?
I found the original answer from peetysmith was correct for me. However, the terminal command they provided to update the PATH variable in Launchd did not work for me (I'm using High Sierra).
I found the below worked:
sudo launchctl config user path /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
You need to restart afterwards and no need to reload the plist file (unless you edited it).
OK - so after a huge amount of searching I finally figured this out. There were actually two things causing me a problem when getting this working and they both related to the way that launchctl executes the work on behalf of the user.
The key things to remember are that launchctl will execute a process on your behalf and that means:
It has it’s own PATH variable (which you need to update with your chromedriver location), and
It executes your script from it’s own working directory, rather than the one your python script lives in. This will give you an issue for any other files you want to include or relative paths you use in your script(s)
To amend launchctl’s path you therefore need to run the following command from the terminal:
launchctl setenv PATH /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Can’t remember if you then needed a restart or not but definitely unload and then reload your plist file
launchctl load ~Library/LaunchAgents/your.own.plist
You should then be good to go.
Fixing the second point is then a simple key-string pair to add yo your plist file
<key>WorkingDirectory</key>
<string>/Users/you/path/to/mypythonfile/</string>
Hope this is helpful for others!

Python Subprocess Class

I am a learning Python and was trying the subprocess class from the tutorial. The tutorial uses MAC OS hence used ls -l . Since i am using Windows OS i used dir -d instead.
import subprocess
subprocess.run(["dir", "-d"])
When ran the code in the terminal it prompts
C:\Users\Farhan Hasant\Desktop\HelloWorld>dir -d
Volume in drive C has no label.
Volume Serial Number is 8296-8904
Directory of C:\Users\Farhan Hasant\Desktop\HelloWorld
File Not Found
Again, when i ran the code using code runner in VS code it shows
[Running] python -u "c:\Users\Farhan Hasant\Desktop\HelloWorld\app.py"
Traceback (most recent call last):
File "c:\Users\Farhan Hasant\Desktop\HelloWorld\app.py", line 3, in <module>
subprocess.run(["dir", "-d"])
File "C:\Users\Farhan Hasant\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 472, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\Farhan Hasant\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 775, in init
restore_signals, start_new_session)
File "C:\Users\Farhan Hasant\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1178, in _execute_child
startupinfo)[![enter image description here][1]][1]
FileNotFoundError: [WinError 2] The system cannot find the file specified
[Done] exited with code=1 in 0.213 seconds
My files
I am confused if I am doing it right. I would really appreciate your input on this. Thank you in advance.
dir is not a real command in Windows, it's something builtin in the "shell" so you need to tell subprocess to launch a shell before attempting to run the command:
import subprocess
subprocess.run(["dir", "/d"],shell=True)
Also, follow #jasonharper comment about using / instead of - for most Windows native commands

How to put geckodriver into PATH? [duplicate]

This question already has answers here:
Selenium using Python - Geckodriver executable needs to be in PATH
(36 answers)
Closed 6 years ago.
I'm on OS Sierra and i'm running Python 3.5.2. I have selenium installed and i'm following a book called "Automate the Boring Tasks With Python"
My code is
from selenium import webdriver
>>> browser = webdriver.Firefox()
I keep receiving the error
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 64, in start
stdout=self.log_file, stderr=self.log_file)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver'
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 "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 135, in __init__
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/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.
I have searched far and wide for solutions to my issue. Many people have the same issue.. but none of the solutions are working. I have geckodriver copied everywhere in my Python folders. I've tried using terminal and I have tried specifying the path within the code and it still gives me errors. I hope somebody can help me. I'm sorry if this is in the wrong format, I don't know what i'm doing.
I faced this same problem and here's how I fixed it:
Download the geckodriver from here
Extract and unzip and move the geckodriver file to /usr/local/bin/ directory
Run python program with selenium Firefox webdriver.
You will need to add the binary to your $PATH. You can do this by:
export PATH=$PATH:/path/to/geckodriver
"I have geckodriver copied everywhere in my Python folders."
Ensure the geckodriver executable is found in one of the paths when you run:
import sys
print sys.path
And the problem should be resolved.

OSError: [Errno 8] Exec format error selenium

Trying to learn how to use selenium, I managed to overcome first error which involved chrome driver not being in the path name but it has thrown up another error.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('/Users/williamneal/Scratch/Titanic/chromedriver')
driver.get("http://www.bbc.com")
The error:
Traceback (most recent call last):
File "<ipython-input-1-84256e62b8db>", line 5, in <module>
driver = webdriver.Chrome('/Users/williamneal/Scratch/Titanic/chromedriver')
File "/Users/williamneal/anaconda/lib/python3.5/site-packages/selenium/webdriver/chrome/webdriver.py", line 62, in __init__
self.service.start()
File "/Users/williamneal/anaconda/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 64, in start
stdout=self.log_file, stderr=self.log_file)
File "/Users/williamneal/anaconda/lib/python3.5/subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "/Users/williamneal/anaconda/lib/python3.5/subprocess.py", line 1544, in _execute_child
raise child_exception_type(errno_num, err_msg)
OSError: [Errno 8] Exec format error
There is a potential solution here, which involves installing Chrome Drivers via Home Brew but that option is not available to me.
Any ideas?
Looks like this is complaining about the format of chromedriver binary.
It might be because of platform and chromedriver format mismatch. For example windows requires chromedriver.exe while there are different formats for linux and mac.
If you don't want to install through package manager, just download chromedriver from https://sites.google.com/a/chromium.org/chromedriver/downloads
Note : Choose file as per your os
Then place it anywhere on the os and pass that path as an argument. You can also set webdriver.chrome.driver environment variable if you don't want to pass the location every time.
FYI you could also encounter this issue if you did not unzip the chromedriver before adding it to your PATH.

PyTesser simple usage error

I've downloaded PyTesser and extracted it.
I was in the pytesser_v0.0.1 folder and tried to run the sample usage code in the python interpreter:
from pytesser import *
print image_file_to_string('fnord.tif')
and the output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pytesser.py", line 44, in image_file_to_string
call_tesseract(filename, scratch_text_name_root)
File "pytesser.py", line 21, in call_tesseract
proc = subprocess.Popen(args)
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
NOTE: I'm in Ubuntu 12.10 with Python 2.7.3
can anyone help me understand this error, and what can I do to fix it ?
This isn't as well documented as it could be, but if you are not on Windows you need to install the tesseract binary for your platform. On Ubuntu and other Debian based Linux distributions, apt-get install tesseract-ocr. Then you can run:
python pytesser.py
which uses the test files phototest.tif, fnord.tif and fonts_test.png to test the library.
For beginners on windows to use pytesseract:
Open command prompt
Type: pip install pytesseract
(this will install pytesseract last version module on your python easily)
Go to this link and download and install tesseract-ocr engine:
https://code.google.com/p/tesseract-ocr/downloads/detail?name=tesseract-ocr-setup-3.02.02.exe&can=2&q=
Now you are ready to use pytesseract
For more information and see code example check this link:
http://www.manejandodatos.es/2014/11/ocr-python-easy/

Categories

Resources