python Selenium GeckoDriver启动Firefox浏览器缓慢

bnlyeluc  于 2024-01-05  发布在  Python
关注(0)|答案(2)|浏览(230)

我最近一直在尝试让Selenium环境重新启动并运行。一切都像它应该的那样工作,脚本执行它们应该的那样,使用不同的Web驱动程序等。
然而,每个驱动程序的初始启动需要2-5分钟的时间,这使得开发和排除脚本故障成为一件痛苦的事情。
我使用Python 3.6和PyCharm,Selenium和pip一起安装。Python 2.7也存在同样的问题。
我已经尝试过了(没有成功):

  • 重新安装Python
  • 重新安装Selenium
  • 通过PyCharm和终端运行脚本
  • 已将Web驱动程序替换为早期和更高版本。
  • 已禁用web驱动程序的代理和JavaScript

下面是我正在运行的脚本。这个简单的脚本运行时间长达5分钟,但工作正常,没有返回任何警告或错误。

  1. from selenium import webdriver
  2. import time
  3. print(time.localtime())
  4. browser = webdriver.Firefox()
  5. browser.get('http://seleniumhq.org/')
  6. print(time.localtime())

字符串
编辑:上面的代码运行一次就会产生下面的geckodriver.log

  1. 1528112695159 geckodriver INFO geckodriver 0.19.1
  2. 1528112695168 geckodriver INFO Listening on 127.0.0.1:3148
  3. 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"
  4. Unable to read VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath
  5. Unable to read VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath
  6. Unable to read VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath
  7. Unable to read VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath
  8. Unable to read VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath
  9. 1528112791808 Marionette INFO Listening on port 3158
  10. 1528112792599 Marionette WARN TLS certificate errors will be ignored for this session
  11. [Parent 36664, Gecko_IOThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
  12. [Parent 36664, Gecko_IOThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
  13. [Child 22672, Chrome_ChildThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
  14. [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
  15. [Child 25068, Chrome_ChildThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
  16. [Child 25068, C*** UTM:SVC TimerManager:registerTimer called after profile-before-change notification. Ignoring timer registration for id: telemetry_modules_ping

h4cxqtbf

h4cxqtbf1#

这些错误信息…

  1. Unable to read VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath
  2. Unable to read VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath
  3. Unable to read VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath
  4. Unable to read VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath
  5. Unable to read VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath

字符串
.意味着有几次尝试读取现有的VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath
您的主要问题是系统中存在os chores

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

解决方案

要加快操作速度,请执行以下步骤:

  • Selenium 升级到当前级别Version 3.12.0
  • GeckoDriver 升级到GeckoDriver v0.20.1级别。
  • 确保 GeckoDriver 二进制文件存在于指定位置。
  • 确保 GeckoDriver 具有非root用户的可执行权限。
  • Firefox 版本升级到 Firefox v60.0.1 级别。
    • 通过 IDE 清理 * 您的 * 项目工作区 *,并仅使用所需的依赖项 * 重建 * 您的项目。
  • 使用CCleaner工具在执行 Test Suite 之前和之后清除所有操作系统杂务。
  • 如果您的基本 Web Client 版本太旧,请通过Revo Uninstaller卸载它,并安装最新的GA和发布版本的 Web Client
  • 进行 * 系统重启 *。
  • 以非root用户身份执行Test
  • 始终在tearDown(){}方法中调用driver.quit(),以优雅地关闭和销毁 WebDriverWeb Client 示例。
    备注:请勿 * 禁用*JavaScript。禁用JavaScript可能会导致网页加载不完整,因为所有网页设计师/开发人员都认为 JavaScript 适用于所有浏览器,并且所有最新的浏览器功能和效果都是使用 JavaScript 实现的。
展开查看全部
w8ntj3qf

w8ntj3qf2#

老线程,但同样的问题,使用C#。
解决方案是在启动selenium之前删除配置文件中“storage”文件夹的内容。
我需要存储在配置文件中的cookie,所以从浏览器中清除不是一个选项。
C#代码以编程方式完成,将其转换为您的特定语言:

  1. private void buttonCleanProfCacheRespectCookies_Click(object sender, EventArgs e)
  2. {
  3. string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  4. string firefoxProfilesPath = Path.Combine(appDataPath, "Mozilla\\Firefox\\Profiles");
  5. // Optional, if you want to see the folder by yourself
  6. //Process.Start("explorer.exe", firefoxProfilesPath);
  7. // The folder name is something cryptic like g7ufgsz3w72n.MySeleniumProfile, so its better to look for it using a partial match
  8. const string PARTIAL_PROFILE_NAME = "Selenium";
  9. deleteStorageContents(firefoxProfilesPath, PARTIAL_PROFILE_NAME);
  10. }
  11. static void deleteStorageContents(string firefoxProfilesPath, string partialProfileName)
  12. {
  13. try
  14. {
  15. // Search for a folder with the partial name inside the specified path
  16. string[] matchingProfiles = Directory.GetDirectories(firefoxProfilesPath, "*" + partialProfileName + "*");
  17. foreach (string profile in matchingProfiles)
  18. {
  19. string storagePath = Path.Combine(profile, "storage");
  20. // Check if the "storage" folder exists before attempting to delete its contents
  21. if (Directory.Exists(storagePath))
  22. {
  23. // Delete all files within the "storage" folder
  24. foreach (string file in Directory.GetFiles(storagePath))
  25. {
  26. File.Delete(file);
  27. }
  28. // Delete all subdirectories within the "storage" folder
  29. foreach (string subDirectory in Directory.GetDirectories(storagePath))
  30. {
  31. Directory.Delete(subDirectory, true);
  32. }
  33. Console.WriteLine($"Contents of 'storage' folder in profile '{profile}' deleted.");
  34. }
  35. else
  36. {
  37. Console.WriteLine($"'storage' folder not found in profile '{profile}'.");
  38. }
  39. }
  40. }
  41. catch (Exception ex)
  42. {
  43. Console.WriteLine("Error: " + ex.Message);
  44. }
  45. }

字符串

展开查看全部

相关问题