使用Selenium 3启动特定的Firefox配置文件 [英] Starting a specific Firefox Profile with Selenium 3
问题描述
我正在尝试从Selenium 2升级到Selenium 3,但是旧的处理方式(非常简单,快速)不再起作用了(而且文档似乎不存在)
I am trying to upgrade from Selenium 2 to Selenium 3 but the old handling, which was pretty easy and fast doesn't work anymore (and the documentation is nonexisting as it seems)
此刻是程序,我想要打开一个配置文件为:SELENIUM的Firefox驱动程序
This is the program at the moment and what I want is to open a Firefox driver with the profile: SELENIUM
不幸的是,它始终无法正常工作因错误而关闭:
Sadly it doesn't work and always shuts down with the Error:
WebDriver.dll中发生了'System.InvalidOperationException'类型的未处理异常
An unhandled exception of type 'System.InvalidOperationException' > occurred in WebDriver.dll
其他信息:腐败的放气流
Additional information: corrupt deflate stream
这是我目前的程序:
public Program()
{
FirefoxOptions _options = new FirefoxOptions();
FirefoxProfileManager _profileIni = new FirefoxProfileManager();
FirefoxDriverService _service = FirefoxDriverService.CreateDefaultService(@"C:\Programme\IMaT\Output\Release\Bin");
_service.FirefoxBinaryPath = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
try
{
if ((_options.Profile = _profileIni.GetProfile("SELENIUM")) == null)
{
Console.WriteLine("SELENIUM PROFILE NOT FOUND");
_profile.SetPreference("network.proxy.type", 0); // disable proxy
_profile = new FirefoxProfile();
}
}
catch
{
throw new Exception("Firefox needs a Profile with \"SELENIUM\"");
}
IWebDriver driver = new FirefoxDriver(_service,_options,new System.TimeSpan(0,0,30));
driver.Navigate().GoToUrl("ld-hybrid.fronius.com");
Console.Write("rtest");
}
static void Main(string[] args)
{
new Program();
}
在不加载配置文件的情况下,它仅适用于新的FirefoxDriver(_service),但该配置文件
Without Loading the Profile it works with just new FirefoxDriver(_service) but the profile is mandatory.
在Selenium 2中,我使用以下代码对其进行了处理:
In Selenium 2 I handled it with this code:
FirefoxProfileManager _profileIni = new FirefoxProfileManager();
// use custom temporary profile
try {
if ((_profile = _profileIni.GetProfile("SELENIUM")) == null)
{
Console.WriteLine("SELENIUM PROFILE NOT FOUND");
_profile.SetPreference("network.proxy.type", 0); // disable proxy
_profile = new FirefoxProfile();
}
}
catch
{
throw new Exception("Firefox needs a Profile with \"SELENIUM\"");
}
_profile.SetPreference("intl.accept_languages", _languageConfig);
_driver = new FirefoxDriver(_profile);
快速而简单,但是由于驱动程序不支持带有服务和配置文件的构造方法,我不这样做真的不知道如何使它正常工作,将不胜感激
Fast and simple, but as the Driver doesn't support a Constructor with service and profile I don't really know how to get this to work, any help would be appreciated
推荐答案
此异常是由于中的错误所致。网络图书馆。
This exception is due to a bug in the .Net library. The code generating the Zip of the profile is failing to provide a proper Zip.
一种解决此问题的方法是重载 FirefoxOptions
One way to overcome this issue would be to overload FirefoxOptions
and use the archiver from .Net framework (System.IO.Compression.ZipArchive) instead of the faulty ZipStorer
:
var options = new FirefoxOptionsEx();
options.Profile = @"C:\Users\...\AppData\Roaming\Mozilla\Firefox\Profiles\ez3krw80.Selenium";
options.SetPreference("network.proxy.type", 0);
var service = FirefoxDriverService.CreateDefaultService(@"C:\downloads", "geckodriver.exe");
var driver = new FirefoxDriver(service, options, TimeSpan.FromMinutes(1));
class FirefoxOptionsEx : FirefoxOptions {
public new string Profile { get; set; }
public override ICapabilities ToCapabilities() {
var capabilities = (DesiredCapabilities)base.ToCapabilities();
var options = (IDictionary)capabilities.GetCapability("moz:firefoxOptions");
var mstream = new MemoryStream();
using (var archive = new ZipArchive(mstream, ZipArchiveMode.Create, true)) {
foreach (string file in Directory.EnumerateFiles(Profile, "*", SearchOption.AllDirectories)) {
string name = file.Substring(Profile.Length + 1).Replace('\\', '/');
if (name != "parent.lock") {
using (Stream src = File.OpenRead(file), dest = archive.CreateEntry(name).Open())
src.CopyTo(dest);
}
}
}
options["profile"] = Convert.ToBase64String(mstream.GetBuffer(), 0, (int)mstream.Length);
return capabilities;
}
}
并按名称获取配置文件的目录:
And to get the directory for a profile by name:
var manager = new FirefoxProfileManager();
var profiles = (Dictionary<string, string>)manager.GetType()
.GetField("profiles", BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(manager);
string directory;
if (profiles.TryGetValue("Selenium", out directory))
options.Profile = directory;
这篇关于使用Selenium 3启动特定的Firefox配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!