代码之家  ›  专栏  ›  技术社区  ›  Rahul

使用python 3以无头模式运行firefox浏览器时出错

  •  0
  • Rahul  · 技术社区  · 7 年前

    我只是试着用无头浏览器运行它,我不明白为什么即使我提供了参数,它也会不断地向我抛出错误。我在这里读到,它需要参数来传递选项。add_argument():- https://seleniumhq.github.io/selenium/docs/api/py/webdriver_firefox/selenium.webdriver.firefox.options.html#module-selenium.webdriver.firefox.options

    from selenium import webdriver
    from selenium.webdriver.firefox.options import Options
    
    
    options = Options.add_argument('-headless')
    browser = webdriver.Firefox(options)
    browser.get('https://intoli.com/blog/email-spy/')
    browser.implicitly_wait(50)
    heading = browser.find_element_by_xpath('//*[@id="heading-breadcrumb"]/div/div/div/h1').text
    print(heading)
    browser.close()
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Lescurel    7 年前

    在调用对象的属性之前,应该先创建对象选项。 有关@property如何工作的更多信息,请参阅此 answer

    # create a new object
    options = Options()
    # calling the property (setter)
    options.add_argument('-headless')
    

    使现代化 此外,您的代码示例似乎还有其他问题。 如果您只想提供firefox_选项,则应将其作为关键字参数,因此必须明确提供:

    browser = webdriver.Firefox(firefox_options=options)