首页 > 解决方案 > WebDriverException:消息:Service C:\Program Files\Mozilla Firefox\firefox.exe 通过 Selenium 使用 DesiredCapabilities 意外退出

问题描述

我需要在我的 Windows 计算机上使用最新版本的 Firefox。因此不想使用默认的 gecko 驱动程序。这是我有多接近。

 import time
 from selenium import webdriver
 from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
 from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

 binary = webdriver.Firefox(executable_path= r'C:\Program Files\Mozilla Firefox\firefox.exe')
 caps = DesiredCapabilities.FIREFOX.copy()

 caps['marionette'] = True

 driver = webdriver.Firefox(firefox_binary=binary,capabilities=caps, executable_path=(os.path.abspath("geckodriver.exe")))

 time.sleep(5)
 driver.get("http://www.google.com")

最新的浏览器使用默认页面启动,但 driver.get()在退出时无法使用 WebDriverException:消息:服务 C:\Program Files\Mozilla Firefox\firefox.exe 意外退出。状态码是: 1. 我该如何解决。

标签: pythonseleniumfirefoxgeckodriverdesiredcapabilities

解决方案


您需要在这里处理几件事:

  • 该参数executable_path用于传递geckodriver二进制文件的绝对路径。
  • 如果Firefox安装在默认位置,则根本不需要传递Firefox二进制文件的绝对路径。
  • 如果您使用Selenium 3.xGeckoDriverFirefox,默认情况下将功能木偶设置为true您不必明确提及。
  • 诱导time.sleep()降低测试执行性能使用WebDriverWait代替。
  • 您的有效代码块将是:

    from selenium import webdriver
    from selenium.webdriver.firefox.options import Options
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    
    binary = r'C:\Program Files\Mozilla Firefox\firefox.exe'
    options = Options()
    options.binary = binary
    cap = DesiredCapabilities().FIREFOX.copy()
    cap["marionette"] = True #optional
    driver = webdriver.Firefox(firefox_options=options, capabilities=cap, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
    driver.get("http://google.com/")
    print ("Firefox Initialized")
    driver.quit()
    
  • 控制台输出:

    Firefox Initialized
    

推荐阅读