使用FirefoxBinary.StartProfile()无头运行Firefox

时间:2018-07-10 16:59:22

标签: c# selenium selenium-webdriver

我有以下问题 我尝试使用FirefoxBinary.StartProfile() headless模式中打开 firefox 。我收到一个错误。 这是我的代码

var path = @"C:\Users\camera\Downloads\FirefoxPortable\App\Firefox\firefox.exe";

var abc = @"C:\Users\camera\Desktop\FirefoxPortable\App\DefaultData\profile\bookmarks.html";
string[] arr2 = { "--headless"};
FirefoxProfile profile = new FirefoxProfile(abc);
FirefoxBinary binary = new FirefoxBinary(path);  
//err here      
binary.StartProfile(profile, "--headless");
var driver = new FirefoxDriver(binary, profile);
driver.Navigate().GoToUrl("http://google.com");

Fbinary.StartProfile(profile, "--headless")行中,我收到了错误消息

An unhandled exception of type 'OpenQA.Selenium.WebDriverException' occurred in WebDriver.dll

Additional information: You must set both the property name and value: XRE_PROFILE_PATH,

我不知道如何解决此问题。我传递了正确的参数顺序

使用C#以某种模式运行firefox可移植程序很困难。我看到Java可以轻松做到这一点。但是我喜欢C#

请帮帮我!谢谢

1 个答案:

答案 0 :(得分:0)

如果您要做的就是以无头模式启动Firefox,则您需要做的就是以下类似的事情:

// WARNING! Code written from memory
// without the benefit of an IDE.
// Syntax may be slightly incorrect,
// or may not even compile.
FirefoxOptions options = new FirefoxOptions();
options.AddArgument("--headless");
// Use this to specify non-standard Firefox install location.
options.BinaryLocation = @"C:\path\to\portable\firefox.exe");
IWebDriver driver = new FirefoxDriver(options);