How To Resolve WebdriverException Geckodriver Executable Needs To Be In Path

When i run below python selenium webdriver source code to open a Firefox browser to run automation test in MacOS, i meet an error message like below.

1. Python Source Code.

    from selenium import webdriver
    browser = webdriver.Firefox()
    browser.get('https://www.google.com') 

2. Error Messages.

Traceback (most recent call last):
  File "/Users/zhaosong/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/com/dev2qa/example/selenium/RunSeleniumWithDifferentWebBrowser.py", line 16, in <module>
    run_webdriver_firefox()
  File "/Users/zhaosong/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/com/dev2qa/example/selenium/RunSeleniumWithDifferentWebBrowser.py", line 11, in run_webdriver_firefox
    browser = webdriver.Firefox()
  File "/Users/zhaosong/anaconda3/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 157, in __init__
    self.service.start()
  File "/Users/zhaosong/anaconda3/lib/python3.6/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.

3. Resolve Method.

The key error message is that Message: ‘geckodriver’ executable needs to be in PATH. After some investigate, i finally find the method to resolve it.

But before any solution to take effect, we should download the related geckodriver executable file from mozilla/geckodriver. Then unzip it to a local folder such as /Users/zhaosong/Documents/WorkSpace/tool.

3.1 Specify The executable_path Parameter Value When Initialize Firefox.

This is the simplest way to fix this problem. Please see below source code. Then the Firefox browser can be started as normal. This method also take effect when you run or debug above python code in Eclipse PyDev plugin.

    from selenium import webdriver
    browser = webdriver.Firefox(executable_path = '/Users/zhaosong/Documents/WorkSpace/tool/geckodriver')
    browser.get('https://www.google.com') 

3.2 Set geckodriver Saved Folder In OS Path Environment Variable.

  1. Open a terminal and run env command to show the PATH environment variable value.
  2. If the geckodriver saved folder do not included in the PATH value. Then run below shell command in the terminal.
    # Go to the current user home directory.
    cd ~
    # Edit .bash_profile file with vim editor.
    vim .bash_profile
    # Press keyboard esc and i key to insert geckodriver saved folder to the PATH environment variable and export PATH.
    export PATH="< geckodriver executable file saved folder>:$PATH"
    # Press keyboard : then wq! to save and quite.
    # Make the PATH environment variable value change take effect.
    source .bash_profile 
  3. Now save above python source code in a file RunFirefox.py and run it in terminal, the Firefox browser will be started as normal.
    python RunFirefox.py

3.3 Use Brew To Install geckodriver In MacOS.

  1. Open a terminal and run below command.
    192:~ $ brew install geckodriver
    ......
    ==> Pouring geckodriver--0.21.0.high_sierra.bottle.tar.gz
    🍺  /usr/local/Cellar/geckodriver/0.21.0: 7 files, 5.4MB
  2. When the command execute successfully, you can run geckodriver --version to check that geckodriver has been installed successfully. And run which geckodriver command to see the geckodriver installation folder.
    192:~$ geckodriver --version
    geckodriver 0.21.0
    ...
    
    192:~$ which geckodriver
    /usr/local/bin/geckodriver
  3. Then you can run the python code to open Firefox browser.
(Visited 272 times, 6 visits today)
READ :   Selenium Webdriver Handle Firefox Or Chrome Download Popup

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.