Running into error while using selenium python? Any suggestions [duplicate] - python

I am using firefox gecko driver to make a bot, I keep getting error messages with my bot and I have found that the source of the message is the:
from selenium import webdriver
browser= webdriver.Firefox()
lines.
I have added every file mentioned in the error message to path including gecko driver, firefox, and the other smaller documents. I am at a complete loss
Here is the error message:
Traceback (most recent call last):
File "/usr/local/bin/fuckobot1.py", line 3, in <module>
browser= webdriver.Firefox()
File "/Users/'myname'/Library/Python/2.7/lib/python/site-packages/selenium/webdriver/firefox/webdriver.py", line 164, in __init__
self.service.start()
File "/Users/'myname'/Library/Python/2.7/lib/python/site-packages/selenium/webdriver/common/service.py", line 76, in start
stdin=PIPE)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 8] Exec format error

This error message...
OSError: [Errno 8] Exec format error
...implies that the GeckoDriver binary which was invoked was not in proper format.
Your main issue is the incompatibility of the GeckoDriver binary format with respect to the underlying Operating System.
As you are on MacOS you need to download geckodriver-v0.23.0-macos.tar.gz from mozilla/geckodriver, untar/unzip it and provide the absolute path of the GeckoDriver through the argument executable_path as follows:
from selenium import webdriver
browser= webdriver.Firefox(executable_path='/path/to/geckodriver')

This may be answer to this issue,
when it shows something like, Your Firefox profile cannot be loaded. It may be missing or inaccessible.
Actual problem is, Firefox 48.0 or greater not works with selenium 3.0.0 or greater version.
so please check your versions.
so, to make it work you need to update with recent selenium and respective geckodriver version too that fixes this issue.
then try basic commands,
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()

Related

How can I solve this selenium-chromedriver error? [duplicate]

I have created a test script to open a url in Eclipse using python and got the following error:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 76, in start
stdin=PIPE)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 769, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 1516, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Applications/Eclipse.app/Contents/MacOS/C:\EclipseWorkspaces\csse120/PythonSeleniumProject/src/PythonSeleniumModule.py", line 13, in <module>
driver = webdriver.Firefox()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 164, in __init__
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 83, 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 read in stack overflow about related topics but none of them answers/solves my problem.
Please advise.
Thank you.
This error message...
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver'
.
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
...implies that your program was unable to locate the GeckoDriver within the mentioned directory.
As per your code trials you have used:
driver = webdriver.Firefox()
As you havn't mentioned the absolute path of the GeckoDriver explicitly, your program searches for the GeckoDriver within the paths mentioned within your underlying Operating System PATH variable and unable to locate.
Solution
As you are on Mac OS X download the latest geckodriver-v0.23.0-macos.tar.gz from mozilla/geckodriver, store it anywhere within your system.
In your program override the paths mentioned in your Operating System PATH variable through the argument executable_path as follows:
from selenium import webdriver
driver = webdriver.Firefox(executable_path='/path/to/geckodriver')
print("Firefox Browser Invoked")
driver.get('http://google.com/')
driver.quit()
The solution above will not work on every machine depending on the absolute path you choose. Also, the absolute paths that are easy to access via any program, e.g. root folder, requires admin permissions.
There's a DriverManager module to every Selenium WebDriver that exists, and you can use it to install the WebDriver on a directory inside PATH variable automatically.
It is important that you do the installing just once. Doing it again will cause errors, and I haven't found how to overcome them yet.
First, install webdriver-manager on your Python environment with
pip install webdriver-manager
or pip3 if you're using Python3.
Do this on your code and it will work fine:
from webdriver_manager.firefox import GeckoDriverManager
try:
driver = webdriver.Firefox()
except Exception:
driver = webdriver.Firefox(GeckoDriverManager().install())
Therefore it will install only once on the machine using the program.
Note: This has some issues with OperaDriver.

Done Numerous research but unable to get the expected output

I have tried running below code in python IDE interpreter as well as in PyCharm installing the module but still getting an error,( I have installed selenium module),
from selenium import webdriver
browser = webdriver.Firefox()
type(browser)
browser.get('http://inventwithpython.com')
------------------------------------------------------------------------------------------
Output:
Traceback (most recent call last):
File "demo.py", line 2, in <module>
browser = webdriver.Firefox()
File "/usr/lib/python3/dist-packages/selenium/webdriver/firefox/webdriver.py", line 164, in __init__
self.service.start()
File "/usr/lib/python3/dist-packages/selenium/webdriver/common/service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
First look at this and install webdriver for your system.
Then add it to your system path (i don't know what is your OS so you have to find the tutorial of this by your self and it's not hard) or for temporary usage add this to your code:
browser=webdriver.Chrome(executable_path=r"C:\path\to\chromedriver.exe")
# Change Chrome to your desired browser and if you are not on Windows it doesn't have .exe

FileNotFoundError: [WinError 2] The system cannot find the file specified though the exact same file worked previously

I have a python script that previously worked fine but now errors, so I am not sure what happened. I am getting the error that:
C:\Users\663255\Desktop>PMI_Tests.py
Traceback (most recent call last):
File "C:\Users\663255\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\common\service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "C:\Users\663255\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\Users\663255\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 990, 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 "C:\Users\663255\Desktop\PMI_Tests.py", line 14, in <module>
driver = webdriver.Firefox(firefox_binary=binary, executable_path=gecko+'.exe')
File "C:\Users\663255\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 142, in __init__
self.service.start()
File "C:\Users\663255\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver.exe' executable needs to be in PATH.
The start of the file looks like:
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import time
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.action_chains import ActionChains
from urllib.request import urlopen
from html.parser import HTMLParser
gecko = os.path.normpath(os.path.join(os.path.dirname(__file__), 'geckodriver'))
binary = FirefoxBinary('C:\Program Files (x86)\Mozilla Firefox\Firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary, executable_path=gecko+'.exe')
class PythonOrgSearch(unittest.TestCase):
#sets up driver to run tests
def setUp(self):
self.driver = driver
I am unsure why the file stopped working, as it had worked previously many times before. also, both python and geckodriver are definitely defined in the path, but when run via the terminal it says that geckodriver is not in the path.
I have a feeling the issue has to do with the way geckodriver (i.e. gecko variable) is defined in a weird way in the code, or something like that. I have viewed other stack overflow posts with similar problems but have not found anything that fixed my problem. I am using Python 3.6.2 if that is helpful. Any insight would be awesome. Thank you!
As a solution to this problem, I hardcoded the full path to geckodriver in the gecko variable, instead of trying to dynamically locate it. I am unsure why the dynamic approach stopped working, but hardcoding the filepath fixed any issues for me.

selenium wont work with Firefox or Chrome

I am trying to learn python web scraping, but I cannot get selenium to work with either browser.
from selenium import webdriver
browser = webdriver.Firefox()
This is all the code I have and I get this for a error.
Traceback (most recent call last):
File "C:\Users\tjhall\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\tjhall\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 1220, 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 "H:\codingpractice\python\python challenge.com.py", line 2, in <module>
browser = webdriver.Firefox()
File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 135, in __init__
self.service.start()
File "C:\Users\tjhall\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.
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00A11350>>
Traceback (most recent call last):
File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
self.stop()
File "C:\Users\tjhall\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
I have tried everything I can find on the internet from adding the path to the code
from selenium import webdriver
browser = webdriver.Firefox("C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
to adding it the the PATH in my environment variables. I can't seem to figure this out...
For both Firefox and Chrome you now need to download geckodriver / chromedriver.These drivers are necessary to communicate between your installed browser and selenium. So you need:
Install selenium for python (pip install selenium)
Download drivers for the browser you want to use (chromedriver, geckodriver, operadriver etc)
Install the browser you want to use on your system (probably already have this)
Now you can add the geckodriver to your path as metioned in this anwser. Or you can set it up directly in your code like this:
Chome:
driver = webdriver.Chrome(executable_path='/path/to/chromedriver.exe')
Firefox:
driver = webdriver.Firefox(executable_path='/opt/geckoDriver/geckodriver.exe')
According to the line in your message
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
you do not have geckodriver.exe. You need to download it from enter link description here, put exe in the directory where Python script is located and try code below:
Please try this code:
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
gecko = os.path.normpath(os.path.join(os.path.dirname(__file__), 'geckodriver'))
binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe')
browser = webdriver.Firefox(firefox_binary=binary,executable_path=gecko+'.exe')
browser.get('http:///www.google.com')
browser.close()

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.

Categories

Resources