Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test-Driven Development with Python Tutorial: Error trying to open Firefox with Selenium on MacOSX

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.

like image 432
DjH Avatar asked Dec 20 '25 14:12

DjH


1 Answers

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
like image 96
DjH Avatar answered Dec 22 '25 04:12

DjH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!