python - WebDriverException : Message: Service C:\Program Files\Mozilla Firefox\firefox. exe 通过 Selenium 使用 DesiredCapativity 意外退出

标签 python selenium firefox geckodriver 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.我该如何出行。

最佳答案

您需要注意以下几件事:

  • 参数executable_path用于传递geckodriver二进制文件的绝对路径
  • 如果 Firefox 安装在默认位置,则无需传递 firefox 的绝对路径强>完全是二进制的。
  • 如果您使用Selenium 3.xGeckoDriverFirefox,则ma​​rionette功能设置为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
    

关于python - WebDriverException : Message: Service C:\Program Files\Mozilla Firefox\firefox. exe 通过 Selenium 使用 DesiredCapativity 意外退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55699330/

相关文章:

java - PhantomJS 和 Selenium Webdriver - 如何清除 session

perl - WWW::Mechanize::Firefox 超时

Python VLC 不会播放音频?

selenium - 未知错误 : Connection refused (Connection refused)

javascript - Phantom JS 没有点击按钮,但如果我使用任何其他浏览器它就可以工作

javascript - 在应用程序管理器中单步执行 JavaScript 代码?

google-chrome - Youtube视频未在Chrome中显示,但在Firefox中显示

Python - 检查列表中的所有元素是否满足不等式

python dateutil unicode警告

找不到 Python 模块错误 "No module named ' ortools'“