python - 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. 我该如何解决。
解决方案
您需要在这里处理几件事:
- 该参数
executable_path
用于传递geckodriver二进制文件的绝对路径。 - 如果Firefox安装在默认位置,则根本不需要传递Firefox二进制文件的绝对路径。
- 如果您使用Selenium 3.x、GeckoDriver和Firefox,默认情况下将功能木偶设置为
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
推荐阅读
- php - 图像和 css 未使用 PHP 在本地 wamp 服务器上加载
- reactjs - 预期 ReactBoostrap 声明或声明。TS1128
- c++ - C++ lock_guard 与 mutex.lock()
- c# - DocuSign Connect 如何配置信封以接收身份验证失败通知?
- angular - 为什么这个 ngx-leaflet 参数不能正常工作?
- kivy - 无法在 kivyMD 中使用全局变量作为 MDLabel 的文本(初学者)
- typescript - 如何将 +1 添加到正则表达式捕获?
- vector-graphics - 支持可变厚度行程
- mysql - Django模板循环多对多迭代查询效率
- java - Corda - 避免发出重复的状态