7

我想知道为什么我会收到以下错误消息以及如何解决它:

Traceback(最近一次调用最后一次):文件“teste.py”,第 30 行,在 <\module>

主要的()

文件“teste.py”,第 24 行,在 main

驱动程序 = connectFirefox(defineOptions())

connectFirefox 中的文件“teste.py”,第 18 行

驱动程序 = webdriver.Firefox(firefox_options=options)

文件“/usr/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py”,第 125 行,在init

如果 options.binary 不是无:

AttributeError:“选项”对象没有属性“二进制”

我的代码:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.chrome.options import Options

def defineOptions():
    options = Options()
    options.add_argument("--headless")
    return options

def connectChrome(options):
    chromeDriverPath = "/usr/bin/chromedriver"
    driver = webdriver.Chrome(chromeDriverPath, chrome_options=options)
    print("Chrome Headless Browser Invoked")
    return driver

def connectFirefox(options):
    driver = webdriver.Firefox(firefox_options=options)
    print("Firefox Headless Browser Invoked")
    return driver

def main():
    #driver = connectChrome(defineOptions())
    driver = connectFirefox(defineOptions())
    driver.get("https://www.archlinux.org/")
    print("Headless Browser closing")
    driver.quit()
#------------------------------------------------------------------------#

main()

我要做的是编写一个代码,让我可以轻松地选择 Chrome 或 Firefox 无头。

4

1 回答 1

13

我解决了我的问题。这里的代码:

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

def connectChrome():
    options = ChromeOptions()
    options.add_argument("--headless")
    chromeDriverPath = "/usr/bin/chromedriver"
    driver = webdriver.Chrome(chromeDriverPath, chrome_options=options)
    print("Chrome Headless Browser Invoked")
    return driver

def connectFirefox():
    options = FirefoxOptions()
    options.add_argument("--headless")
    driver = webdriver.Firefox(firefox_options=options)
    print("Firefox Headless Browser Invoked")
    return driver

def main():
    #driver = connectChrome()
    driver = connectFirefox()
    driver.get("https://www.archlinux.org/")
    print("Headless Browser closing")
    driver.quit()
#------------------------------------------------------------------------#
main()
于 2018-05-18T19:56:44.077 回答