A Campos
A Campos

Reputation: 803

Path issues with Selenium and Geckodriver

I am new to programing, did some python courses and am trying to apply what I've been learning.

I am running a macOS Sierra and have python2 and 3 installed in my machine, even though I just wanted to use the python3, but my previous course had instructed me to start with python2, which I don't know if it was a bad thing.

Anyway, taking the Automate the Boring Stuff with Python course (which uses python3) I ran into this code:

#! python3
from selenium import webdriver
browser = webdriver.Firefox()

and got the following error message:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 64, in start
    stdout=self.log_file, stderr=self.log_file)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 1551, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/Alex/Anaconda/Templates/selenium_firefox.py", line 3, in <module>
    browser = webdriver.Firefox()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 135, in __init__
    self.service.start()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/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. 

Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x1029777f0>>
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 163, in __del__
    self.stop()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 135, in stop
    if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
[Finished in 0.501s]

I found an answer that seems to solve my problem here: Selenium using Python - Geckodriver executable needs to be in PATH

But I can't quite understand how to manipulate PATHs in my computer or how to organize my files in a way that the computer works.

I executed the following code on my terminal (as instructed in the other query): exportPATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step

But it does not make any sense to me nor did it work. I also tried taking the Geckodriver file from the downloads (where it was originally) and placing it inside my Anaconda folder.

Anyway, I am quite sure the problem is that I don't really know how the computer organize itself, thus cannot properly address the code.

Therefore, I would like to ask a solution for my specific case and a reference text, tutorial, video or anything alike that I could use to better understand how all this works (I still didn't find any good material on that matter).

Thanks in advance!

Upvotes: 1

Views: 4149

Answers (3)

dingo
dingo

Reputation: 156

I had the same error I figured it out, here are the steps:

Mac:

  1. Download geckodriver for MacOS from the following link:

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

  2. Go to Terminal and type the following command to know the path of Python:

    echo $PATH
    

    Generally the path will be /usr/local/bin.

  3. Copy the geckodriver from the downloads folder to the path that you obtained in step 2. Use the following command:

    cp downloads /usr/local/bin 
    

Note: Sometimes while doing step 3, you may get a permission denied error, To resolve this error you should use sudo in front of the command like so:

sudo cp downloads /usr/local/bin

You'll have to type your account password after that. FYI, sudo makes you run the commands as administrator.

Upvotes: 4

Nathan Gisvold
Nathan Gisvold

Reputation: 61

You can always hardcode the diver location:

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"):

Upvotes: 1

MSJ
MSJ

Reputation: 154

The link you found for UNIX should work. Do you have a space between export and PATH ? Your copy has no space between so that wouldn't work. If it's the same in UNIX as in Windows for geckodriver in the Python path, then you could try to:

cp geckodriver.exe \path\to\Python\

Then geckodriver is stored in the base path of Python and is thus initialized automatically.

Upvotes: 1

Related Questions