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

百般思念 提交于 2021-01-28 20:06:08

问题


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?


回答1:


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:

  • How to make Firefox headless programmatically in Selenium with Python?


来源:https://stackoverflow.com/questions/65291985/attributeerror-options-object-has-no-attribute-binary-error-invoking-headle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!