Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium using Python - Geckodriver executable needs to be in PATH

I'm new to programming and started with Python about two months ago and am going over Sweigart's Automate the Boring Stuff with Python text. I'm using IDLE and already installed the Selenium module and the Firefox browser.

Whenever I tried to run the webdriver function, I get this:

from selenium import webdriver browser = webdriver.Firefox() 

Exception:

Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00000249C0DA1080>> Traceback (most recent call last):   File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__     self.stop()   File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop     if self.process is None: AttributeError: 'Service' object has no attribute 'process' Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00000249C0E08128>> Traceback (most recent call last):   File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__     self.stop()   File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop     if self.process is None: AttributeError: 'Service' object has no attribute 'process' Traceback (most recent call last):   File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 64, in start     stdout=self.log_file, stderr=self.log_file)   File "C:\Python\Python35\lib\subprocess.py", line 947, in __init__     restore_signals, start_new_session)   File "C:\Python\Python35\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#11>", line 1, in <module>     browser = webdriver.Firefox()   File "C:\Python\Python35\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 135, in __init__     self.service.start()   File "C:\Python\Python35\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. 

I think I need to set the path for geckodriver, but I am not sure how, so how would I do this?

like image 635
tadm123 Avatar asked Oct 23 '16 21:10

tadm123


Video Answer


2 Answers

selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

First of all you will need to download latest executable geckodriver from here to run latest Firefox using Selenium

Actually, the Selenium client bindings tries to locate the geckodriver executable from the system PATH. You will need to add the directory containing the executable to the system path.

  • On Unix systems you can do the following to append it to your system’s search path, if you’re using a Bash-compatible shell:

      export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step 
  • On Windows you will need to update the Path system variable to add the full directory path to the executable geckodriver manually or command line** (don't forget to restart your system after adding executable geckodriver into system PATH to take effect)**. The principle is the same as on Unix.

Now you can run your code same as you're doing as below :-

from selenium import webdriver  browser = webdriver.Firefox() 

selenium.common.exceptions.WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line

The exception clearly states you have installed Firefox some other location while Selenium is trying to find Firefox and launch from the default location, but it couldn't find it. You need to provide explicitly Firefox installed binary location to launch Firefox as below :-

from selenium import webdriver from selenium.webdriver.firefox.firefox_binary import FirefoxBinary  binary = FirefoxBinary('path/to/installed firefox binary') browser = webdriver.Firefox(firefox_binary=binary) 

https://github.com/mozilla/geckodriver/releases

For Windows:

Download the file from GitHub, extract it, and paste it in Python file. It worked for me.

https://github.com/mozilla/geckodriver/releases

For me, my path path is:

C:\Users\MYUSERNAME\AppData\Local\Programs\Python\Python39 
like image 124
Saurabh Gaur Avatar answered Sep 18 '22 12:09

Saurabh Gaur


This solved it for me.

from selenium import webdriver driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe') driver.get('http://inventwithpython.com') 
like image 36
Nesa Avatar answered Sep 19 '22 12:09

Nesa