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?


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

This solved it for me.

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')
driver.get('http://inventwithpython.com')

This steps solved it for me on Ubuntu and Firefox 50.

  1. Download geckodriver

  2. Copy geckodriver to folder /usr/local/bin

You do not need to add:

firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = '/usr/bin/firefox'
browser = webdriver.Firefox(capabilities=firefox_capabilities)

I see the discussions still talk about the old way of setting up geckodriver by downloading the binary and configuring the path manually.

This can be done automatically using webdriver-manager

pip install webdriver-manager

Now the above code in the question will work simply with the below change,

from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

The answer by @saurabh solves the issue, but it doesn't explain why Automate the Boring Stuff with Python doesn't include those steps.

This is caused by the book being based on Selenium 2.x and the Firefox driver for that series does not need the Gecko driver. The Gecko interface to drive the browser was not available when Selenium was being developed.

The latest version in the Selenium 2.x series is 2.53.6 (see e.g. [these answers][2], for an easier view of the versions).

The [2.53.6 version page][3] doesn't mention Gecko at all. But since version 3.0.2 the documentation [explicitly states][4] you need to install the Gecko driver.

If after an upgrade (or install on a new system), your software that worked fine before (or on your old system) doesn't work anymore and you are in a hurry, pin the Selenium version in your virtualenv by doing

pip install selenium==2.53.6

but of course the long term solution for development is to setup a new virtualenv with the latest version of selenium, install the Gecko driver and test if everything still works as expected.

But the major version bump might introduce other API changes that are not covered by your book, so you might want to stick with the older Selenium, until you are confident enough that you can fix any discrepancies between the Selenium 2 and Selenium 3 API yourself.

[2]: https://stackoverflow.com/a/40746017/1307905) [3]: https://pypi.python.org/pypi/selenium/2.53.6 [4]: https://pypi.python.org/pypi/selenium#drivers