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 an automation test in macOS, I meet an error message selenium.common.exceptions.WebDriverException:Message: ‘geckodriver’ executable needs to be in PATH. This article will tell you how to fix it.

1. Python Source Code.

  1. Below is the example python source code.
    from selenium import webdriver
    browser = webdriver.Firefox()
    browser.get('https://www.google.com')

2. Error Messages.

  1. When executing the above python source code, it shows the below error message.
    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.

  1. The key error message is that Message: ‘geckodriver’ executable needs to be in PATH. After some investigation, I finally find the method to resolve it.
  2. But before any solution takes 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.

  1. This is the simplest way to fix this problem. Please see below source code.
    from selenium import webdriver
    browser = webdriver.Firefox(executable_path = '/Users/zhaosong/Documents/WorkSpace/tool/geckodriver')
    browser.get('https://www.google.com')
  2. Now the Firefox browser can be started as normal. This method also takes effect when you run or debug the above python code in the Eclipse PyDev plugin.

3.2 Set Geckodriver Saved Folder In OS Path Environment Variable.

  1. Open a terminal and run the command env to show the PATH environment variable value.
  2. If the PATH environment variable value does not include the geckodriver executable file saved folder. Then run the 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 the above python source code in a file such as RunFirefox.py and run it in a 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 the brew install geckodriver 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 executes successfully, you can run geckodriver --version to check that Geckodriver has been installed successfully. And run the command which geckodriver 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 the Firefox browser.

4. Use The Python Webdriver-Manager Library To Resolve The Error.

  1. There is a new way to set up geckodriver without the need to download the executable file and set it in the OS PATH environment variable value.
  2. The python webdriver-manager library can help you to do it easily.
  3. Run the command pip install webdriver-manager in a terminal to install the library.
    $ pip install webdriver-manager
    Collecting webdriver-manager
      Downloading webdriver_manager-3.3.0-py2.py3-none-any.whl (16 kB)
    Collecting crayons
      Downloading crayons-0.4.0-py2.py3-none-any.whl (4.6 kB)
    Requirement already satisfied: requests in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from webdriver-manager) (2.22.0)
    Collecting configparser
      Downloading configparser-5.0.2-py3-none-any.whl (19 kB)
    Collecting colorama
      Downloading colorama-0.4.4-py2.py3-none-any.whl (16 kB)
    Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from requests->webdriver-manager) (3.0.4)
    Requirement already satisfied: idna<2.9,>=2.5 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from requests->webdriver-manager) (2.8)
    Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from requests->webdriver-manager) (1.25.3)
    Requirement already satisfied: certifi>=2017.4.17 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from requests->webdriver-manager) (2019.6.16)
    Installing collected packages: colorama, crayons, configparser, webdriver-manager
    Successfully installed colorama-0.4.4 configparser-5.0.2 crayons-0.4.0 webdriver-manager-3.3.0
  4. You should change the source code as below.
    # Import webdriver module.
    from selenium import webdriver
    
    # Import GeckoDriverManager module.
    from webdriver_manager.firefox import GeckoDriverManager
    
    # Install the GeckoDriverManager to run FireFox web browser.
    driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

5. How To Install Geckodriver In Anaconda.

  1. If you run your python script in an anaconda python virtual environment, you can install the geckodriver by a conda command in your activated python virtual environment.
  2. Open anaconda, click the Environments menu on the left side.
  3. Select your python virtual environment, click the green triangle button at the end of the virtual environment, click the Open Terminal menu item in the popup menu list.
  4. Then run the command conda install -c conda-forge geckodriver in the terminal.
    $ conda install -c conda-forge geckodriver
    Collecting package metadata (current_repodata.json): done
    Solving environment: done
    
    
    ==> WARNING: A newer version of conda exists. <==
      current version: 4.8.3
      latest version: 4.9.2
    
    Please update conda by running
    
        $ conda update -n base -c defaults conda
    
    
    
    ## Package Plan ##
    
      environment location: /Users/songzhao/opt/anaconda3/envs/env_ipython_example
    
      added / updated specs:
        - geckodriver
    
    
    The following packages will be downloaded:
    
        package                    |            build
        ---------------------------|-----------------
        ca-certificates-2020.12.5  |       h033912b_0         138 KB  conda-forge
        certifi-2020.12.5          |   py37hf985489_1         143 KB  conda-forge
        geckodriver-0.29.0         |       h970cd5f_0         2.2 MB  conda-forge
        libcxx-11.1.0              |       habf9029_0         1.0 MB  conda-forge
        openssl-1.1.1j             |       hbcf498f_0         1.9 MB  conda-forge
        python_abi-3.7             |          1_cp37m           4 KB  conda-forge
        ------------------------------------------------------------
                                               Total:         5.4 MB
    
    The following NEW packages will be INSTALLED:
    
      geckodriver        conda-forge/osx-64::geckodriver-0.29.0-h970cd5f_0
      python_abi         conda-forge/osx-64::python_abi-3.7-1_cp37m
    
    The following packages will be UPDATED:
    
      ca-certificates    pkgs/main::ca-certificates-2020.6.24-0 --> conda-forge::ca-certificates-2020.12.5-h033912b_0
      certifi               pkgs/main::certifi-2020.6.20-py37_0 --> conda-forge::certifi-2020.12.5-py37hf985489_1
      libcxx                         pkgs/main::libcxx-10.0.0-1 --> conda-forge::libcxx-11.1.0-habf9029_0
      openssl              pkgs/main::openssl-1.1.1g-h1de35cc_0 --> conda-forge::openssl-1.1.1j-hbcf498f_0
    
    
    Proceed ([y]/n)? y
    
    
    Downloading and Extracting Packages
    libcxx-11.1.0        | 1.0 MB    | ##################################### | 100% 
    geckodriver-0.29.0   | 2.2 MB    | ##################################### | 100% 
    openssl-1.1.1j       | 1.9 MB    | ##################################### | 100% 
    certifi-2020.12.5    | 143 KB    | ##################################### | 100% 
    ca-certificates-2020 | 138 KB    | ##################################### | 100% 
    python_abi-3.7       | 4 KB      | ##################################### | 100% 
    Preparing transaction: done
    Verifying transaction: done
    Executing transaction: done
    
  5. After installing the geckodriver in anaconda, you can run the command which geckodriver in the terminal to get the geckodriver installation directory.
    $ which geckodriver
    /Users/songzhao/opt/anaconda3/envs/env_ipython_example/bin/geckodriver
  6. Run the command geckodriver --version to get its version.
    $ geckodriver --version
    geckodriver 0.29.0
    
    The source code of this program is available from
    testing/geckodriver in https://hg.mozilla.org/mozilla-central.
    
    This program is subject to the terms of the Mozilla Public License 2.0.
    You can obtain a copy of the license at https://mozilla.org/MPL/2.0/.

3 thoughts on “How To Resolve WebdriverException Geckodriver Executable Needs To Be In Path”

Leave a Comment

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.