3
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?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Xia
  • 156
  • 2
  • 9

3 Answers3

3

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
2

The --headless argument works fine in Firefox (geckodriver) these days.

If you're getting the error mentioned in the title, then you're probably accidentally creating or passing a Chrome-based Options object rather than a Firefox-based Options object.

To avoid that mistake, it's best to create an import alias for both of them so that they're easier to distinguish.

from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox.options import Options as FirefoxOptions

chrome_options = ChromeOptions()
chrome_options.add_argument('--headless')
chrome_driver = webdriver.Chrome(executable_path = r"..\mypath\chromedriver.exe", options=chrome_options)

firefox_options = FirefoxOptions()
firefox_options.add_argument('--headless')
firefox_driver = webdriver.Firefox(executable_path = r"..\mypath\geckodriver.exe", options=firefox_options)
Pikamander2
  • 7,332
  • 3
  • 48
  • 69
0

This 4 lines helped me:

from selenium.webdriver.firefox.options import Options as FirefoxOptions

...

    options = FirefoxOptions()
    options.add_argument("--headless")
    driver = webdriver.Firefox(firefox_options=options)

If it still gives an error you can try to substitute fourth line to driver = webdriver.Firefox(options=options)

Reference - Unable to invoke firefox headless

Kyo
  • 61
  • 6