Search code examples
pythonseleniumselenium-webdrivergeckodriverfirefox-headless

AttributeError: 'Options' object has no attribute 'binary' error invoking Headless Firefox using GeckoDriver through Selenium


options = FirefoxOptions()
options.add_argument("--headless")


driver = webdriver.Firefox(firefox_options=options, executable_path='/Users/toprak/Desktop/geckodriver') 
driver.get("https://twitter.com/login?lang=en")

When I try to run my code, I get this error:

Warning (from warnings module):
  File "/Users/toprak/Desktop/topla.py", line 19
    driver = webdriver.Firefox(firefox_options=options, executable_path='/Users/toprak/Desktop/geckodriver')
DeprecationWarning: use options instead of firefox_options
Traceback (most recent call last):
  File "/Users/toprak/Desktop/topla.py", line 19, in <module>
    driver = webdriver.Firefox(firefox_options=options, executable_path='/Users/toprak/Desktop/geckodriver')
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 137, in __init__
    if options.binary is not None:
AttributeError: 'Options' object has no attribute 'binary'

When I delete the lines which are about options and take out "firefox_options=options", the code works fine. What should I do to fix this?


Solution

  • Instead of using firefox_options object you need to use options object. Additionally you need to use the headless attribute. So your effective code block will be:

    options = FirefoxOptions()
    options.headless = True
    
    driver = webdriver.Firefox(executable_path='/Users/toprak/Desktop/geckodriver', options=options) 
    driver.get("https://twitter.com/login?lang=en")
    

    References

    You can find a couple of relevant detailed discussions in: