2

I'm working through "Automate the Boring Stuff with Python" and am running Python 3.6 with a Mac running OS X v. 10.9.5. My version of Firefox is 50.0.

Whenever I type in (per the instructions in chapter 11):

>>> from selenium import webdriver
>>> browser = webdriver.Firefox()

I get the following error:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 74, in start
    stdout=self.log_file, stderr=self.log_file)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 707, in __init__
    restore_signals, start_new_session)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1326, 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 "<pyshell#18>", line 1, in <module>
    browser = webdriver.Firefox()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 140, in __init__
    self.service.start()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 81, 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 searched this error on this site and others, but most of the solutions offered are Windows-specific (including the post referenced in the comment below), or do not seem to work on my Mac machine when applied.

Any suggestions on what I should do?

Please keep in mind that I'm a total novice, and so less than familiar with the terminal. Thank you in advance for your help.

Oligarch
  • 29
  • 3
  • 1
    Possible duplicate of [Selenium using Python - Geckodriver executable needs to be in PATH](http://stackoverflow.com/questions/40208051/selenium-using-python-geckodriver-executable-needs-to-be-in-path) – martianwars Dec 29 '16 at 18:51
  • As I mentioned in my post, I did check on this website for previous posts on the topic, and tried the solutions offered on the post your reference, but did not have any success with them. The post referenced was from a Windows user, whereas I'm using Mac. Furthermore, the posting instructions in that thread explicitly requested posters post answers rather than requests for clarification or additional questions. – Oligarch Dec 29 '16 at 19:21
  • 1
    Did you try one of the comments? "On Mac: `brew install geckodriver`"? – martianwars Dec 29 '16 at 19:23
  • If that doesn't work, it's best you continue the discussion on that page with the author of the question. This can be marked as an exact duplicate – martianwars Dec 29 '16 at 19:26
  • Yes, I did try that, and got the error: -bash: brew: command not found The OP on the other thread was running Windows and I'm running Mac. How is it an exact duplicate? – Oligarch Dec 29 '16 at 19:34
  • Haven't you ever used `brew`? You should get more used to the terminal it seems :) ! Different OS doesn't make a huge difference in this case – martianwars Dec 29 '16 at 19:36
  • See this http://stackoverflow.com/questions/20381128/how-to-install-homebrew-on-os-x – martianwars Dec 29 '16 at 19:37
  • Thanks, but unfortunately despite installing homebrew, and running the "brew install geckodriver" command, I still get the same error as outlined in my post... – Oligarch Dec 29 '16 at 20:53
  • You may need to restart your terminal for it to recognize that geckodriver is now in its path – Lucas Tierney Dec 29 '16 at 22:42

2 Answers2

3

I delved into the source code for webdriver and the Firefox driver: I had to give the Firefox() constructor a direct path to the geckodriver binary, like

self.browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
Anthony Joseph
  • 235
  • 1
  • 2
  • 10
  • Firefox launches, but throws a NameError: name 'self' is not defined'. I removed 'self' and ran the rest of the command, which launched firefox. `browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')` – ficestat Feb 15 '20 at 17:08
0

Thanks all for your replies.

Per the comments, installing Homebrew and running the

brew install geckodriver

command did not alone solve the problem. Restarting Terminal also didn't solve things.

I dug around on a few other posts, and tried opening the /usr/bin and /usr/local/bin files through the Mac Finder, and manually copied the geckodriver file into the folder. The first time I entered:

browser = webdriver.Firefox()

into the Python IDLE, I got the following warning/exceptions:

Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x101a960b8>>
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 173, in __del__
    self.stop()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 145, 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 0x1071b3eb8>>
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 173, in __del__
    self.stop()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 145, in stop
    if self.process is None:
AttributeError: 'Service' object has no attribute 'process'

but the browser opened. When I ran it again, I was able to follow along as in the book without any errors or exceptions.

It seems that this has worked. However, it feels wrong to have done all this through the Mac finder rather than Terminal. I'm still very new to all this, so if there's anything I did that will damage my system, I'd appreciate any pointers. Thanks for your help.

Oligarch
  • 29
  • 3