3

I'm following the tutorial here and I cannot get selenium to open Firefox. I have tried both:

from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://localhost:8000')

assert 'Django' in browser.title

and

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

browser = webdriver.Firefox(firefox_binary=FirefoxBinary(
    firefox_path='/Applications/Firefox.app/Contents/MacOS/firefox-bin'
))

assert 'Django' in browser.title

and

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

browser = webdriver.Firefox(firefox_binary=FirefoxBinary(
    firefox_path='/Applications/Firefox'
))

assert 'Django' in browser.title

With each version I get the same error message in console:

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 "functional_tests.py", line 5, in <module>
    firefox_path='/Applications/Firefox.app/Contents/MacOS/firefox-bin'
  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 0x10312ed30>>
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'

I've exhausted every seemingly relevant search I can think of and have yet to find a solution. The version of Firefox I'm directing to (45) should be compatible with Selenium according to the Tutorial... I can't find anything I missed in the tutorial that may be causing this either.

2
  • What version of Selenium are you using? Commented Oct 24, 2016 at 17:48
  • @OCary I used the command sudo pip3 install --upgrade selenium so I'm not sure exactly but I would think it'd the most recent stable version (3.0.1?) Commented Oct 24, 2016 at 18:00

1 Answer 1

3

Finally found a solution to this. Apparently starting with Selenium v3 Firefox is no longer natively supported. So, (still using Firefox v45) I found a solution here and integrated it with my own code. (I will try to explain but this is the first time I've ever used Selenium so for a better understanding I'd recommend reading the linked article.)

Essentially it seems that the Selenium webdriver needs a bit of configuration, which I did like so.

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

# Bring up the capabilities object for editing and call on FIREFOX
caps = DesiredCapabilities.FIREFOX

# Specify the exact binary of Firefox you want it to point to
caps["binary"] = "/Applications/Firefox.app/Contents/MacOS/firefox-bin"

# Specify geckodriver location
geckodriver = '/Users/<username>/Downloads/geckodriver'

# Now we can call the Firefox webdriver, but this time with specified flags for the capabilities and geckodriver exec location
browser = webdriver.Firefox(capabilities=caps, executable_path=geckodriver)
browser.get('http://localhost:8000')

assert 'Django' in browser.title
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this! You'll want to add something like the following before the last line, to load the page before you inspect its title: browser.get('http://localhost:8000')
@fureigh np, and yea I forgot to come back and edit it when I saw that I pasted over that. good catch!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.