Skip to main content
 首页 » 编程设计

python之Selenium GeckoDriver 启动 Firefox 浏览器的速度很慢

2024年12月31日13daizhj

我最近一直在尝试重新启动并运行 Selenium 环境。一切正常,脚本执行正常,使用不同的网络驱动程序等。

但是,每个驱动程序的初始启动需要 2-5 分钟才能启动。这让我的脚本开发和故障排除变得很痛苦。

我使用带有 PyCharm 的 Python 3.6 和通过 pip 安装的 Selenium。 Python 2.7 也存在同样的问题。

到目前为止我尝试过的事情(没有成功):

  • 重新安装 Python
  • 重新安装 Selenium
  • 通过 PyCharm 和终端运行脚本
  • 用早期和更新版本替换了网络驱动程序。
  • 禁用网络驱动程序的代理和 javascript

这是我正在运行的。这个简单的脚本最多需要 5 分钟才能运行,但可以正常运行并且不会返回任何警告或错误。

from selenium import webdriver 
import time 
print(time.localtime()) 
browser = webdriver.Firefox() 
browser.get('http://seleniumhq.org/') 
print(time.localtime()) 

编辑: 单次运行上面的代码会产生以下 geckodriver.log

1528112695159   geckodriver INFO    geckodriver 0.19.1 
1528112695168   geckodriver INFO    Listening on 127.0.0.1:3148 
1528112747876   mozrunner::runner   INFO    Running command: "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile" "C:\\Users\\victorsk\\AppData\\Local\\Temp\\rust_mozprofile.ULuflXGoq9rE" 
Unable to read VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath 
Unable to read VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath 
Unable to read VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath 
Unable to read VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath 
Unable to read VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath 
1528112791808   Marionette  INFO    Listening on port 3158 
1528112792599   Marionette  WARN    TLS certificate errors will be ignored for this session 
[Parent 36664, Gecko_IOThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346 
[Parent 36664, Gecko_IOThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346 
[Child 22672, Chrome_ChildThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346 
[Child 22672, Chrome_ChildThread] WARNING: pipe error: 109: file z:/build/build/sr[Parent 36664, Gecko_IOThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346 
[Child 25068, Chrome_ChildThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346 
[Child 25068, C*** UTM:SVC TimerManager:registerTimer called after profile-before-change notification. Ignoring timer registration for id: telemetry_modules_ping 

请您参考如下方法:

这些错误信息...

Unable to read VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath 
Unable to read VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath 
Unable to read VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath 
Unable to read VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath 
Unable to read VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath 

...表示已多次尝试从 C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath 读取现有的 VR 路径注册表。

您的主要问题是您的系统中存在操作系统杂务。

  • 我们未知您的Selenium 客户端版本。
  • 您的 GeckoDriver 版本是 v0.19.1。
  • 我们未知您的 Firefox 版本。

解决方案

要加快操作,请按照以下步骤操作:

  • 将 Selenium 升级到当前水平 Version 3.12.0 .
  • 将 GeckoDriver 升级到 GeckoDriver v0.20.1水平。
  • 确保 GeckoDriver 二进制文件存在于指定位置。
  • 确保 GeckoDriver 具有非根用户的可执行权限。
  • 将 Firefox 版本升级到 Firefox v60.0.1 级别。
  • 清理您的项目工作区,通过您的IDE 并重建您的项目,仅使用所需的依赖项。
  • 使用CCleaner用于在执行您的测试套件之前和之后清除所有操作系统杂务的工具。
  • 如果您的基本 Web Client 版本太旧,请通过 Revo Uninstaller 卸载它。并安装最新的 GA 和发布版本的 Web Client。
  • 系统重启。
  • 以非根用户身份执行您的测试。
  • 始终在 tearDown(){} 方法中调用 driver.quit() 以关闭和销毁 WebDriver 和 Web 客户端 实例优雅。

Note: Do not disable JavaScript. Disabling JavaScript might lead to a incomplete loading of the webpage as all the web designers/developers believes JavaScript is available in every browser and all the recent browser features and effects are implemented using JavaScript.