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

问题描述 投票:0回答:2

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

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

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

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

  • 重新安装Python
  • 重新安装硒
  • 通过 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
python selenium firefox selenium-webdriver geckodriver
2个回答
1
投票

这些错误消息...

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

...意味着多次尝试读取现有的

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

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

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

解决方案

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

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

注意:不要禁用JavaScript。禁用 JavaScript 可能会导致网页加载不完整,因为所有网页设计者/开发人员都相信 JavaScript 在每个浏览器中都可用,并且所有最新的浏览器功能和效果都是使用 JavaScript 实现的。


0
投票

旧线程但同样的问题,使用 C#。

解决方案是在启动 selenium 之前删除配置文件中“storage”文件夹的内容。

我需要将 Cookie 存储在个人资料中,因此无法从浏览器中进行清理。

以编程方式执行此操作的 C# 代码,将其转换为您的特定语言:

private void buttonCleanProfCacheRespectCookies_Click(object sender, EventArgs e)
{
    string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    string firefoxProfilesPath = Path.Combine(appDataPath, "Mozilla\\Firefox\\Profiles");

    // Optional, if you want to see the folder by yourself
    //Process.Start("explorer.exe", firefoxProfilesPath);


    // The folder name is something cryptic like g7ufgsz3w72n.MySeleniumProfile, so its better to look for it using a partial match
    const string PARTIAL_PROFILE_NAME = "Selenium";

    deleteStorageContents(firefoxProfilesPath, PARTIAL_PROFILE_NAME);
}

static void deleteStorageContents(string firefoxProfilesPath, string partialProfileName)
{
    try
    {
        // Search for a folder with the partial name inside the specified path
        string[] matchingProfiles = Directory.GetDirectories(firefoxProfilesPath, "*" + partialProfileName + "*");

        foreach (string profile in matchingProfiles)
        {
            string storagePath = Path.Combine(profile, "storage");

            // Check if the "storage" folder exists before attempting to delete its contents
            if (Directory.Exists(storagePath))
            {
                // Delete all files within the "storage" folder
                foreach (string file in Directory.GetFiles(storagePath))
                {
                    File.Delete(file);
                }

                // Delete all subdirectories within the "storage" folder
                foreach (string subDirectory in Directory.GetDirectories(storagePath))
                {
                    Directory.Delete(subDirectory, true);
                }

                Console.WriteLine($"Contents of 'storage' folder in profile '{profile}' deleted.");
            }
            else
            {
                Console.WriteLine($"'storage' folder not found in profile '{profile}'.");
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error: " + ex.Message);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.