FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver' with GeckoDriver and Python in MAC OS

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.

Upvotes: 3

Views: 9635

Answers (2)

Fornazari
Fornazari

Reputation: 51

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.

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193298

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()
    

Upvotes: 3

Related Questions