2
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")

当我尝试运行我的代码时,我收到此错误:

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'

当我删除关于选项的行并取出“firefox_options=options”时,代码工作正常。我应该怎么做才能解决这个问题?

4

1 回答 1

3

firefox_options您需要使用对象而不是使用options对象。此外,您需要使用该headless属性。因此,您的有效代码块将是:

options = FirefoxOptions()
options.headless = True

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

参考

您可以在以下位置找到一些相关的详细讨论:

于 2020-12-14T15:57:59.377 回答