633

I 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?

starball
  • 20,030
  • 7
  • 43
  • 238
tadm123
  • 8,294
  • 7
  • 28
  • 44
  • 5
    Please, see my answer on similar question [here](http://stackoverflow.com/q/40186299/3022982) – Andrew Oct 23 '16 at 21:45
  • I'm putting the geckodriver.exe in the Python/Python35 directory so it has the same path and I'm getting even more problems. – tadm123 Oct 23 '16 at 22:18
  • 124
    On Mac: `brew install geckodriver` – Nostalg.io Nov 15 '16 at 07:43
  • 2
    I found that running it through the Chrome browser is a little faster than on Firefox, you'll just have to download the `chromedriver` for this. – tadm123 Nov 27 '16 at 23:22
  • Note: there's [Testcafe](https://devexpress.github.io/testcafe/documentation/getting-started/) that got open-sourced recently. It doesn't require any browser plugins, they're built-in. I wanted to use Selenium but that looks like an interesting alternative. – Ehvince Dec 09 '16 at 13:07
  • [Check out following link for solution](https://stackoverflow.com/questions/40048940/geckodriver-executable-needs-to-be-in-path) – sottany Jun 28 '17 at 07:30
  • On Ubuntu follow these steps : https://www.liquidweb.com/kb/how-to-install-selenium-tools-on-ubuntu-18-04/ – gxmad Mar 11 '21 at 07:03
  • any windows answer please? 90% are linux answer – greendino Apr 22 '22 at 02:50
  • @greendino [Very good multi language answer here](/a/38676858), also relevant depending on the chosen solution is [Adding a directory to the PATH environment variable in Windows](/q/9546324) – cachius May 21 '22 at 23:11
  • You can also specify the path to the `geckodriver` executable via `Selenium::WebDriver::Firefox::Service.driver_path = "path/to/geckodriver"`. – Joshua Pinter May 26 '22 at 22:55

38 Answers38

460

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)
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • 10
    Thanks but I set the my `geckodriver.exe` on the `C:\Python\Python35\selenium` directory and I set the path like you described but it's giving me the error below: – tadm123 Oct 24 '16 at 02:57
  • `'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'` – tadm123 Oct 24 '16 at 02:58
  • That means path problem has been resolved, this clearly states you didn't installed firefox in the default location, so you need provide explicitly firefox installed location to launch – Saurabh Gaur Oct 24 '16 at 03:05
  • 5
    Thanks @Saurabh Gaur, it's working now. I added the path of Firefox to the system variables manually and it's all working. Takes a little bit of time to launch but I'm guessing that's normal. Thanks! – tadm123 Oct 24 '16 at 03:28
  • I had exactly the same issue as the thread starter(the original issue). However simply adding the path of the .exe did not work. The .exe had to be in the python35 folder. – Michael Johnson Oct 25 '16 at 10:32
  • 4
    I got the error "WebDriverException: Message: Failed to start browser: permission denied" at first when I started specifying the firefox binary path, but restarting the computer (Windows 10) resolved the problem. - Just in case anyone else is hitting the same problem as me. – NoSuchElephantException Nov 03 '16 at 19:46
  • 1
    on osx I put it `/usr/local/bin` – Harry Moreno Dec 01 '16 at 23:43
  • 3
    What is the binary? Does that mean executable? – User Dec 08 '16 at 08:24
  • 15
    In addition to this answer, I would like to expand on setting the `PATH` in unix environment. You can set it in code since you don't need it system wide: `os.environ["PATH"] += os.pathsep + 'path/to/dir/containing/geckodriver/'` Or simply keep the geckodriver binary in the directory that is already in your path: `mv geckodriver /usr/local/bin` – dsalaj Mar 23 '17 at 07:47
  • 1
    Where to download `geckodriver` from? The github repo consists of two files only... – Alex Sep 13 '17 at 15:04
  • @Alex you can [download latest `geckodriver ` v018.0 from here](https://github.com/mozilla/geckodriver/releases/tag/v0.18.0) – Saurabh Gaur Sep 14 '17 at 10:06
  • Weirdly, it won't work from /usr/bin even though that folder is in PATH. Had to put it in my home directory. – wordsforthewise Jul 19 '18 at 17:57
  • 1
    Windows user: Don't forget to **restart** after changing the path. IMO the most important part. I've struggled an hour until I found this answer :-/ – ascripter Jul 27 '19 at 13:13
  • You can get geckodriver in ubuntu 20 using command bellow: sudo apt install firefox-geckodriver – Overlord Sep 04 '20 at 21:05
  • @ascripter I added it to the path and restarted but I'm still getting the same error as in the OP's question. – Have a nice day Apr 06 '21 at 16:39
  • I'm getting `firefox_binary has been deprecated` -warning. I think this answer contains the currently working approach: https://stackoverflow.com/a/58404246/1548275 – Jari Turkia Feb 09 '22 at 15:39
225

This solved it for me.

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')
driver.get('http://inventwithpython.com')
Nesa
  • 2,895
  • 2
  • 12
  • 19
  • If you get wrong permission try to remove [r'] from the path just "excecutable_path='path\to\your'diretory'" – Darius Nov 04 '17 at 08:17
  • @adityarawat what operating system are you on? – Nesa May 09 '18 at 13:50
  • but now i am getting OSError instead of wrong permissions. I somehow managed to copy geckodriver to /usr/local/bin. but now this new error is killing me – aditya rawat May 09 '18 at 14:06
  • @adityarawat you can download geckodriver independently from here: https://github.com/mozilla/geckodriver/releases extract it with tar and make it executable with chmod +x, it doesn't have to be in /usr/local/bin, you just have to specify your path to it – Nesa May 09 '18 at 14:15
  • just to be clear i have downloaded arm7hf.tar file and extracted it as well and added it to the path using the command `export PATH=$PATH:geckodriver` (it is extracted in Desktop). But it didn't help either. I still get OSError[errno 8 ] – aditya rawat May 09 '18 at 14:22
  • @adityarawat and did you make it executable? If you do it with my method, then you don't have to export the path to it – Nesa May 09 '18 at 14:25
  • guess what. It is now working on idle but not on pycharm(it is completly useless). And even if it did start firefox it did not assert the title i want it to. But i think it solved the problem. Thanks @Nesa. but it still display wrong permissions in pycharm even though i provided all the permissions. – aditya rawat May 09 '18 at 14:33
  • It throws `selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities` – 0x48piraj Jun 05 '18 at 20:28
  • It worked on Cent OS 7. I need to write `executable_path` explicitly as keyword argument. No keyword argument causes an error. – Kei Minagawa Sep 28 '18 at 12:11
  • I am working on Windows using Jupyter Lab. i found this to be the best solution. I copied the geckodriver.exe to my project for convenience. Now I do not even have to add it to PATH or give the directory path explicitly. – DotPi Nov 30 '18 at 06:44
  • Works, but I did have to put the .exe in a local drive (did not work on a network place) – lui Jan 27 '19 at 15:04
  • It worked for me, I just downloaded the geckodriver from the link and added its path. Thanks bro !!! – Harsh Gupta Apr 16 '20 at 11:25
  • What platform? Windows? – Peter Mortensen Nov 06 '20 at 06:36
  • python 3: executable_path has been deprecated, please pass in a Service object – JRichardsz Dec 04 '22 at 17:32
  • python 3.10, windows, firefox: TypeError: WebDriver.__init__() got an unexpected keyword argument 'executable_path'. This needs a `Service` object, try https://stackoverflow.com/a/76580522 – John Jun 29 '23 at 11:18
145

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)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andrea Perdicchia
  • 2,786
  • 1
  • 20
  • 19
  • 1
    In your code you can not add the capability variable – Andrea Perdicchia Jan 12 '17 at 07:47
  • Would you happen to know how to set the download directory for Firefox? I added the following question [Set Firefox Preferences](http://stackoverflow.com/questions/41644381/python-set-firefox-preferences-for-selenium-download-location). Any help would be much appreciated. – d84_n1nj4 Jan 16 '17 at 16:00
  • In Debian or Ubuntu you must use apt command for install Firefox. For Windows I've no idea sorry – Andrea Perdicchia Jan 16 '17 at 16:40
  • Thanks. After applying this answer, I further took this solution for handling a follow-up issue: https://stackoverflow.com/questions/43713445/selenium-unable-to-find-a-matching-set-of-capabilities-despite-driver-being-in – HackNone Jul 12 '17 at 09:03
  • Thanks, Pycharm wasn't finding geckodriver although it was in home and in the project folder itself, but after moving it to /usr/local/bin it worked perfectly – Ronald Das May 11 '18 at 08:19
  • Here is what worked for me : https://www.liquidweb.com/kb/how-to-install-selenium-tools-on-ubuntu-18-04/ – gxmad Mar 11 '21 at 07:04
71

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())
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Navarasu
  • 8,209
  • 2
  • 21
  • 32
  • Does Safari or even Internet Explorer have a similar driver manager? – Mischief_Monkey May 21 '20 at 14:32
  • Or using `pip3`? – Peter Mortensen Nov 06 '20 at 07:07
  • It sort of works, but I think `geckodriver` needs to be in the path. Temporary, like: `export PATH=$PATH:/home/embo/.wdm/drivers/geckodriver/linux64/v0.28.0` – Peter Mortensen Nov 06 '20 at 07:20
  • 3
    Awesome! This is so much easier! – Tmfwang Mar 05 '21 at 10:20
  • 1
    Thanks heaps. This was the only thing that worked. I was running a cron job that spawned a program to a thread that ran selenium. – MagicLAMP Jan 13 '22 at 13:06
  • Part of the transcript from ***the first run*** of `GeckoDriverManager().install()` is something like *"Trying to download new driver from https://github.com/mozilla/geckodriver/releases/download/v0.30.0/geckodriver-v0.30.0-linux64.tar.gz. Driver has been saved in cache [/home/mortensen/.wdm/drivers/geckodriver/linux64/v0.30.0]"* – Peter Mortensen Mar 03 '22 at 02:02
  • 1
    This helped thenk you. so cool – snipher marube Oct 10 '22 at 12:02
  • The parameter 'executable_path' got deprecated in Selenium 4. To avoid deprecation warning do: webdriver.Firefox(service=webdriver.firefox.service.Service(GeckoDriverManager().install())) – maratbn Mar 07 '23 at 04:00
42

On macOS with Homebrew already installed, you can simply run the Terminal command:

brew install geckodriver

Because Homebrew already did extend the PATH there isn’t any need to modify any startup scripts.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
roskakori
  • 3,139
  • 1
  • 30
  • 29
38

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, for an easier view of the versions).

The 2.53.6 version page doesn't mention Gecko at all. But since version 3.0.2 the documentation explicitly states 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anthon
  • 69,918
  • 32
  • 186
  • 246
24

To set up geckodriver for Selenium Python:

It needs to set the geckodriver path with FirefoxDriver as the below code:

self.driver = webdriver.Firefox(executable_path = 'D:\Selenium_RiponAlWasim\geckodriver-v0.18.0-win64\geckodriver.exe')

Download geckodriver for your suitable OS (from https://github.com/mozilla/geckodriver/releases) → Extract it in a folder of your choice → Set the path correctly as mentioned above.

I'm using Python 3.6.2 and Selenium WebDriver 3.4.3 on Windows 10.

Another way to set up geckodriver:

i) Simply paste the geckodriver.exe under /Python/Scripts/ (in my case the folder was: C:\Python36\Scripts)
ii) Now write the simple code as below:

self.driver = webdriver.Firefox()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
21

If you are using Anaconda, all you have to do is activate your virtual environment and then install geckodriver using the following command:

conda install -c conda-forge geckodriver
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Rodolfo Alvarez
  • 972
  • 2
  • 10
  • 18
21

Ubuntu 18.04+ and the newest release of geckodriver

This should also work for other Unix-like varieties as well.

export GV=v0.30.0
wget "https://github.com/mozilla/geckodriver/releases/download/$GV/geckodriver-$GV-linux64.tar.gz"
tar xvzf geckodriver-$GV-linux64.tar.gz
chmod +x geckodriver
sudo cp geckodriver /usr/local/bin/

For Mac update to:

geckodriver-$GV-macos.tar.gz
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
jmunsch
  • 22,771
  • 11
  • 93
  • 114
13

The easiest way for Windows!

Download the latest version of geckodriver from here. Add the geckodriver.exe file to the Python directory (or any other directory which already in PATH). This should solve the problem (it was tested on Windows 10).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jalles10
  • 419
  • 4
  • 8
  • Windows Server 2019 - after adding path to geckodriver.exe in system it not worked but after copying geckodiriver.exe to python path it works ! Thank You : ) – Jakub Ujvvary Oct 26 '20 at 12:54
11

geckodriver is not installed by default.

geckodriver

Output:

Command 'geckodriver' not found, but it can be installed with:

sudo apt install firefox-geckodriver

The following command not only installs it, but it also puts it in the executable PATH.

sudo apt install firefox-geckodriver

The problem is solved with only a single step. I had exactly the same error as you and it was gone as soon as I installed it. Go ahead and give it a try.

which geckodriver

Output:

/usr/bin/geckodriver

geckodriver

Output:

1337    geckodriver    INFO    Listening on 127.0.0.1:4444
^C
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
10

For versions Ubuntu 16.04 (Xenial Xerus) and later you can do:

For Firefox:
sudo apt-get install firefox-geckodriver

For Chrome:
sudo apt-get install chromium-chromedriver

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Maheep
  • 767
  • 10
  • 6
9

Steps for Mac

The simple solution is to download GeckoDriver and add it to your system PATH. You can use either of the two approaches:

Short Method

  1. Download and unzip Geckodriver.

  2. Mention the path while initiating the driver:

    driver = webdriver.Firefox(executable_path='/your/path/to/geckodriver')
    

Long Method

  1. Download and unzip Geckodriver.

  2. Open .bash_profile. If you haven't created it yet, you can do so using the command: touch ~/.bash_profile. Then open it using: open ~/.bash_profile

  3. Considering GeckoDriver file is present in your Downloads folder, you can add the following line(s) to the .bash_profile file:

    PATH="/Users/<your-name>/Downloads/geckodriver:$PATH"
    export PATH
    

By this you are appending the path to GeckoDriver to your System PATH. This tells the system where GeckoDriver is located when executing your Selenium scripts.

  1. Save the .bash_profile and force it to execute. This loads the values immediately without having to reboot. To do this you can run the following command:

source ~/.bash_profile

  1. That's it. You are done! You can run the Python script now.
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Umang Sardesai
  • 762
  • 6
  • 14
  • 3
    I was able to download `geckodriver` with Homebrew: `brew install geckodriver` and then initiate Firefox via: `driver = webdriver.Firefox(executable_path="/usr/local/bin/geckodriver")` – P A N Mar 29 '17 at 14:07
8

Some additional input/clarification:

The following suffices as a resolution for Windows 7, Python 3.6, and Selenium 3.11:

dsalaj's note for another answer for Unix is applicable to Windows as well; tinkering with the PATH environment variable at the Windows level and restart of the Windows system can be avoided.

(1) Download geckodriver (as described in this thread earlier) and place the (unzipped) geckdriver.exe at X:\Folder\of\your\choice

(2) Python code sample:

import os;
os.environ["PATH"] += os.pathsep + r'X:\Folder\of\your\choice';

from selenium import webdriver;
browser = webdriver.Firefox();
browser.get('http://localhost:8000')
assert 'Django' in browser.title

Notes:

(1) It may take about 10 seconds for the above code to open up the Firefox browser for the specified URL.

(2) The Python console would show the following error if there's no server already running at the specified URL or serving a page with the title containing the string 'Django':

selenium.common.exceptions.WebDriverException: Message: Reached error page: about:neterror?e=connectionFailure&u=http%3A//localhost%3A8000/&c=UTF-8&f=regular&d=Firefox%20can%E2%80%9

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Snidhi Sofpro
  • 479
  • 7
  • 10
6

I've actually discovered you can use the latest geckodriver without putting it in the system path. Currently I'm using

https://github.com/mozilla/geckodriver/releases/download/v0.12.0/geckodriver-v0.12.0-win64.zip

Firefox 50.1.0

Python 3.5.2

Selenium 3.0.2

Windows 10

I'm running a VirtualEnv (which I manage using PyCharm, and I assume it uses Pip to install everything).

In the following code I can use a specific path for the geckodriver using the executable_path parameter (I discovered this by having a look in Lib\site-packages\selenium\webdriver\firefox\webdriver.py ). Note I have a suspicion that the order of parameter arguments when calling the webdriver is important, which is why the executable_path is last in my code (the second to last line off to the far right).

You may also notice I use a custom Firefox profile to get around the sec_error_unknown_issuer problem that you will run into if the site you're testing has an untrusted certificate. See How to disable Firefox's untrusted connection warning using Selenium?

After investigation it was found that the Marionette driver is incomplete and still in progress, and no amount of setting various capabilities or profile options for dismissing or setting certificates was going to work. So it was just easier to use a custom profile.

Anyway, here's the code on how I got the geckodriver to work without being in the path:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True

#you probably don't need the next 3 lines they don't seem to work anyway
firefox_capabilities['handleAlerts'] = True
firefox_capabilities['acceptSslCerts'] = True
firefox_capabilities['acceptInsecureCerts'] = True

# In the next line I'm using a specific Firefox profile because
# I wanted to get around the sec_error_unknown_issuer problems with the new Firefox and Marionette driver
# I create a Firefox profile where I had already made an exception for the site I'm testing
# see https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles#w_starting-the-profile-manager

ffProfilePath = 'D:\Work\PyTestFramework\FirefoxSeleniumProfile'
profile = webdriver.FirefoxProfile(profile_directory=ffProfilePath)
geckoPath = 'D:\Work\PyTestFramework\geckodriver.exe'
browser = webdriver.Firefox(firefox_profile=profile, capabilities=firefox_capabilities, executable_path=geckoPath)
browser.get('http://stackoverflow.com')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Roochiedoor
  • 887
  • 12
  • 19
  • I got SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes... And I have to change gecko path to epath = r'C:\Users\step_\Documents\mtg_buyer\geckodrivers\geckodriver.exe'. Maybe the reason is that I'm using a Chinese Windows 10? – Endle_Zhenbo Jun 28 '17 at 00:47
6

A new way to avert the error is using Conda environments.

Use conda install -c conda-forge geckodriver and you do not have to add anything to the path or edit the code!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aman Bagrecha
  • 406
  • 4
  • 9
  • I tried this in VSCode, was able to run pytest with webdriver(for Firefox) with Selenium. – M2014 Dec 30 '21 at 16:55
5

You can solve this issue by using a simple command if you are on Linux

  1. First, download (https://github.com/mozilla/geckodriver/releases) and extract the ZIP file

  2. Open the extracted folder

  3. Open the terminal from the folder (where the geckodriver file is located after extraction)

    Enter image description here

  4. Now run this simple command on your terminal to copy the geckodriver into the correct folder:

     sudo cp geckodriver /usr/local/bin
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tanmoy Bhowmick
  • 1,305
  • 15
  • 20
4

It's really rather sad that none of the books published on Selenium/Python and most of the comments on this issue via Google do not clearly explain the pathing logic to set this up on Mac (everything is Windows!). The YouTube videos all pickup at the "after" you've got the pathing setup (in my mind, the cheap way out!). So, for you wonderful Mac users, use the following to edit your Bash path files:

touch ~/.bash_profile; open ~/.bash_profile*

Then add a path something like this....

# Setting PATH for geckodriver
PATH=“/usr/bin/geckodriver:${PATH}”
export PATH

# Setting PATH for Selenium Firefox
PATH=“~/Users/yourNamePATH/VEnvPythonInterpreter/lib/python2.7/site-packages/selenium/webdriver/firefox/:${PATH}”
export PATH

# Setting PATH for executable on Firefox driver
PATH=“/Users/yournamePATH/VEnvPythonInterpreter/lib/python2.7/site-packages/selenium/webdriver/common/service.py:${PATH}”
export PATH*

This worked for me.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
JustASteve
  • 41
  • 1
4

Consider installing a containerized Firefox:

docker pull selenium/standalone-firefox
docker run --rm -d -p 5555:4444 --shm-size=2g selenium/standalone-firefox

Connect using webdriver.Remote:

driver = webdriver.Remote('http://localhost:5555/wd/hub', DesiredCapabilities.FIREFOX)
driver.set_window_size(1280, 1024)
driver.get('https://toolbox.googleapps.com/apps/browserinfo/')
driver.save_screenshot('info.png')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Max Malysh
  • 29,384
  • 19
  • 111
  • 115
3

I'm using Windows 10 and this worked for me:

  1. Download geckodriver from here. Download the right version for the computer you are using.
  2. Unzip the file you just downloaded and cut/copy the ".exe" file it contains
  3. Navigate to C:{your python root folder}. Mine was C:\Python27. Paste the geckodriver.exe file in this folder.
  4. Restart your development environment.
  5. Try running the code again. It should work now.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lone Ronin
  • 2,530
  • 1
  • 19
  • 31
3
from webdriverdownloader import GeckoDriverDownloader # vs ChromeDriverDownloader vs OperaChromiumDriverDownloader
gdd = GeckoDriverDownloader()
gdd.download_and_install()
#gdd.download_and_install("v0.19.0")

This will get you the path to your gekodriver.exe on Windows.

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'C:\\Users\\username\\\bin\\geckodriver.exe')
driver.get('https://www.amazon.com/')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
InLaw
  • 2,537
  • 2
  • 21
  • 33
3

For MacBook users:

Step 1:

Open this link and copy that Homebrew path, paste it in terminal and install it.

Step 2:

brew install geckodriver

Step 3:

pip install webdriver-manager
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
anusha.V
  • 71
  • 2
2

Selenium answers this question in their DESCRIPTION.rst file:

Drivers
=======

Selenium requires a driver to interface with the chosen browser. Firefox, for example, requires geckodriver <https://github.com/mozilla/geckodriver/releases>_, which needs to be installed before the below examples can be run. Make sure it's in your PATH, e. g., place it in /usr/bin or /usr/local/bin.

Failure to observe this step will give you an error `selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

Basically just download the geckodriver, unpack it and move the executable to your /usr/bin folder.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peter Graham
  • 2,467
  • 2
  • 24
  • 29
2

For Windows users

Use the original code as it's:

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

Then download the driver from: mozilla/geckodriver

Place it in a fixed path (permanently)... As an example, I put it in:

C:\Python35

Then go to the environment variables of the system. In the grid of "System variables" look for the Path variable and add:

;C:\Python35\geckodriver

geckodriver, not geckodriver.exe.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Minions
  • 5,104
  • 5
  • 50
  • 91
2

If you use a virtual environment and Windows 10 (maybe it's the same for other systems), you just need to put geckodriver.exe into the following folder in your virtual environment directory:

...\my_virtual_env_directory\Scripts\geckodriver.exe

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
apet
  • 958
  • 14
  • 16
  • Exactly... installing geckodriver inside the environment's lib(Unix) or Scripts(Windows) directory helps solve this problem while using a virtual environment. – Olfredos6 Jun 19 '20 at 17:44
1

On macOS v10.12.1 (Sierra) and Python 2.7.10, this works for me:

def download(url):
    firefox_capabilities = DesiredCapabilities.FIREFOX
    firefox_capabilities['marionette'] = True
    browser = webdriver.Firefox(capabilities=firefox_capabilities,
                                executable_path=r'/Users/Do01/Documents/crawler-env/geckodriver')
    browser.get(url)
    return browser.page_source
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hamid Zandi
  • 2,714
  • 24
  • 32
1

On Raspberry Pi I had to create it from the ARM driver and set the geckodriver and log path in file webdriver.py:

sudo nano /usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py
def __init__(self, firefox_profile=None, firefox_binary=None,
             timeout=30, capabilities=None, proxy=None,
             executable_path="/PATH/gecko/geckodriver",
             firefox_options=None,
             log_path="/PATH/geckodriver.log"):
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Create what? From [an ARM driver](https://www.youtube.com/watch?v=1Dax90QyXgI&t=17m54s)? From [the ARM driver](https://www.youtube.com/watch?v=1Dax90QyXgI&t=19m05s) (is there more than one)? What is *"ARM driver"*? Can you elaborate? – Peter Mortensen Nov 06 '20 at 06:43
  • OK, the OP has left the building: *Last seen more than 4 years ago* – Peter Mortensen Apr 05 '22 at 20:33
1

For me it was enough just to install geckodriver in the same environment:

brew install geckodriver

And the code was not changed:

from selenium import webdriver
browser = webdriver.Firefox()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Olesya M
  • 29
  • 4
1

I developed a script on Linux desktop, to deploy on my headless server. I needed to update running on Windows but got this same error for a couple of reasons:

"selenium.common.exceptions.sessionnotcreatedexception: 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"

This is Windows; I needed to install Firefox, which I probably goofed up because it was lagging and I had two installations progressing for a time.

Once Firefox was installed I found a confirmed solution (eg https://stackoverflow.com/a/42122284) instructing me to use executable_path:

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')

That parameter is unrecognised since a couple of years ago. Instead I need a Service instance. I add this alongside my options, which were required because Firefox still could not be found (which my impatience double installing might be blamed for):

from selenium import webdriver
from selenium.webdriver import FirefoxOptions
from selenium.webdriver.firefox.service import Service
opts.binary_location = r"C:\Program Files\Mozilla Firefox\firefox.exe"
service = Service(r"C:\Users\pythonista\PycharmProjects\world_eater\geckodriver.exe")
driver = webdriver.Firefox(service=service, options=opts)

This is my only selenium project presently, it can manage its own geckodriver.

John
  • 6,433
  • 7
  • 47
  • 82
0

Visit Gecko Driver and get the URL for the Gecko driver from the Downloads section.

Clone this repository: https://github.com/jackton1/script_install.git

cd script_install

Run

./installer --gecko-driver https://github.com/mozilla/geckodriver/releases/download/v0.18.0/geckodriver-v0.25.0-linux64.tar.gz
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jackotonye
  • 3,537
  • 23
  • 31
0

I am using Windows 10 and Anaconda 2. I tried setting the system path variable, but it didn't work out. Then I simply added geckodriver.exe file to the Anaconda 2/Scripts folder and everything works great now.

For me the path was:

C:\Users\Bhavya\Anaconda2\Scripts
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bhavya Ghai
  • 75
  • 1
  • 5
0

If you want to add the driver paths on Windows 10:

  1. Right click on the "This PC" icon and select "Properties"

    Enter image description here

  2. Click on “Advanced System Settings”

  3. Click on “Environment Variables” at the bottom of the screen

  4. In the “User Variables” section highlight “Path” and click “Edit”

  5. Add the paths to your variables by clicking “New” and typing in the path for the driver you are adding and hitting enter.

  6. Once you done entering in the path, click “OK”

  7. Keep clicking “OK” until you have closed out all the screens

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Russ Thomas
  • 938
  • 4
  • 13
  • 23
0
  1. Ensure you have the correct version of the driver (geckodriver), x86 or 64.
  2. Ensure you are checking the right environment. For example, the job is running in a Docker container, whereas the environment is checked on the host OS.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Wen
  • 11
  • 4
0

It is also possible to do echo PATH (Linux) and just move geckodriver to the folder of your liking. If a system (not virtual environment) folder is the target, the driver becomes globally accessible.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aramakus
  • 1,910
  • 2
  • 11
  • 22
0

On Windows 10 it works for me downloading the geckodriver.exe. I just had to update Firefox.

Below the code that I used:

from selenium import webdriver
driver = webdriver.Firefox(
    executable_path=r'C:\Users\Usuario\Desktop\Automate the boring stuff with python exercises\Web Scraping\geckodriver.exe')
driver.get('http://inventwithpython.com')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

This error message...

FileNotFoundError: [WinError 2] The system cannot find the file specified

...implies that your program was unable to locate the specified file and while handling the exception the following exception occurred:

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

... which implies that your program was unable to locate the GeckoDriver in the process of initiating/spawnning a new Browsing Context i.e. Firefox Browser session.


You can download the latest GeckoDriver from mozilla / geckodriver, unzip/untar and store the GeckoDriver binary/executable anywhere with in your system passing the absolute path of the GeckoDriver through the key executable_path as follows:

from selenium import webdriver

driver = webdriver.Firefox(executable_path='/path/to/geckodriver')
driver.get('http://google.com/')

In case is not installed at the default location (i.e. installed at a custom location) additionally you need to pass the absolute path of firefox binary through the attribute binary_location as follows:

# An Windows example
from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:\WebDrivers\geckodriver.exe')
driver.get('http://google.com/')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Manual steps to install geckodriver on Ubuntu

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

  • Download the latest version of "geckodriver-vX.XX.X-linux64.tar.gz"

  • Unarchive the tarball (tar -xvzf geckodriver-vX.XX.X-linux64.tar.gz)

  • Give executable permissions to geckodriver (chmod +x geckodriver)

  • Move the geckodriver binary to /usr/local/bin or any location on your system PATH.

Script to install geckodriver on Ubuntu:

#!/bin/bash

INSTALL_DIR="/usr/local/bin"

json=$(curl -s https://api.github.com/repos/mozilla/geckodriver/releases/latest)
url=$(echo "$json" | jq -r '.assets[].browser_download_url | select(contains("linux64"))')
curl -s -L "$url" | tar -xz
chmod +x geckodriver
sudo mv geckodriver "$INSTALL_DIR"
echo "installed geckodriver binary in $INSTALL_DIR"

This answer was entirely copied from: Corey Goldberg's answer to How to install geckodriver in Ubuntu?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
victorkolis
  • 780
  • 13
  • 13
0

The major changes of selenium 4.10.0 mean that you'll no longer see the Geckodriver error message if you upgrade your selenium version. selenium has a built-in driver manager (now fully out of beta) that will automatically download geckodriver for you if it's not found on your system PATH. Even the method arg executable_path has been removed from webdriver.Firefox(), as seen below:

https://github.com/SeleniumHQ/selenium/commit/9f5801c82fb3be3d5850707c46c3f8176e3ccd8e

Changes_in_selenium_4_10_0

Now, this is all you need to launch Firefox, even if it is not on your System Path:

from selenium import webdriver
driver = webdriver.Firefox()
# ...
driver.quit()

To customize Firefox, use the options and service args:

from selenium import webdriver
from selenium.webdriver.firefox.service import Service

service = Service()
options = webdriver.FirefoxOptions()
driver = webdriver.Firefox(service=service, options=options)
# ...
driver.quit()

(As before, geckodriver will be downloaded automatically if it is not found on your system PATH.)

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48