python Selenium GeckoDriver启动Firefox浏览器缓慢

bnlyeluc  于 5个月前  发布在  Python
关注(0)|答案(2)|浏览(93)

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

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

h4cxqtbf

h4cxqtbf1#

这些错误信息…

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
您的主要问题是系统中存在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#代码以编程方式完成,将其转换为您的特定语言:

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);
    }
}

字符串

相关问题