我最近一直在尝试重新启动并运行 Selenium 环境。一切都按预期运行,脚本按预期执行,使用不同的网络驱动程序等。
但是,每个驱动程序的首次启动需要 2-5 分钟才能启动。这使得我的脚本的开发和故障排除变得很痛苦。
我使用带有 PyCharm 的 Python 3.6 和通过 pip 安装的 Selenium。 Python 2.7 也存在同样的问题。
到目前为止我尝试过的事情(没有成功):
这就是我正在运行的。这个简单的脚本最多需要 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
...意味着多次尝试读取现有的
VR Path Registry from C:\Users\victorsk\AppData\Local\openvr\openvrpaths.vrpath
。
您的主要问题是系统中存在操作系统杂务。
要加快操作速度,请按照以下步骤操作:
Test
。driver.quit()
方法中调用 tearDown(){}
来优雅地关闭并销毁 WebDriver 和 Web Client 实例。注意:不要禁用JavaScript。禁用 JavaScript 可能会导致网页加载不完整,因为所有网页设计者/开发人员都相信 JavaScript 在每个浏览器中都可用,并且所有最新的浏览器功能和效果都是使用 JavaScript 实现的。
旧线程但同样的问题,使用 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);
}
}