C# 使用自定义Firefox二进制位置和无头Firefox选项
我正在尝试编写C#代码来启动Mozilla Firefox,浏览网站并自动输入表单。我可以让它正常运行,而不必无头,但现在我希望将我的代码转换为运行无头Firefox浏览器 如果通过NuGet安装了最新版本的Selenium和Firefox驱动程序,并且geckodriver的最新版本位于适当的文件夹位置,则以下代码将起作用 要让这段代码打开一个无头Mozilla Firefox,需要做些什么C# 使用自定义Firefox二进制位置和无头Firefox选项,c#,selenium-webdriver,geckodriver,C#,Selenium Webdriver,Geckodriver,我正在尝试编写C#代码来启动Mozilla Firefox,浏览网站并自动输入表单。我可以让它正常运行,而不必无头,但现在我希望将我的代码转换为运行无头Firefox浏览器 如果通过NuGet安装了最新版本的Selenium和Firefox驱动程序,并且geckodriver的最新版本位于适当的文件夹位置,则以下代码将起作用 要让这段代码打开一个无头Mozilla Firefox,需要做些什么 using OpenQA.Selenium; using OpenQA.Selenium.Firefo
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
namespace GoogleSearch
{
class LaunchFirefox
{
static void Main(string[] args)
{
//Start Firefox Gecko Driver Service from Custom Location
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(@"C:\GoogleSearch");
//Force the CMD prompt window to automatically close and suppress diagnostic information
service.HideCommandPromptWindow = true;
service.SuppressInitialDiagnosticInformation = true;
//Launch Mozilla Firefox from custom location
service.FirefoxBinaryPath = @"C:\Firefox\firefox.exe";
//Initialize new Firefox Driver with the above service arguments
FirefoxDriver driver = new FirefoxDriver(service);
//Navigate to the Google website
driver.Navigate().GoToUrl("https://www.google.com");
//Automate custom Google Search Submission
driver.FindElement(By.Name("q")).SendKeys("Stack Overflow");
}
}
}
我尝试插入Firefox选项,但该选项似乎不可用
在尝试向firefox驱动程序初始化行添加选项时,出现以下错误:
错误CS1503参数2:无法从“OpenQA.Selenium.Firefox.FirefoxOptions”转换为“OpenQA.Selenium.Firefox.FirefoxProfile”
任何协助都将不胜感激
我正在运行以下软件:
- 视窗7
- Visual Studio社区版2017
- Mozilla Firefox 61.0.1
- 壁虎驱动程序0.21.0
--headless
选项传递到Firefox命令行来完成的,因此您需要的代码类似于以下代码:
// DISCLAIMER WARNING! The below code was written from
// memory, without benefit of an IDE. It may not be entirely
// correct, and may not even compile without modification.
//Start Firefox Gecko Driver Service from Custom Location
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(@"C:\GoogleSearch");
//Force the CMD prompt window to automatically close and suppress diagnostic information
service.HideCommandPromptWindow = true;
service.SuppressInitialDiagnosticInformation = true;
//Launch Mozilla Firefox from custom location
FirefoxOptions options = new FirefoxOptions();
options.BrowserExecutableLocation = @"C:\Firefox\firefox.exe";
options.AddArgument("--headless");
//Initialize new Firefox Driver with the above service and options
FirefoxDriver driver = new FirefoxDriver(service, options);
//Navigate to the Google website
driver.Navigate().GoToUrl("https://www.google.com");
//Automate custom Google Search Submission
driver.FindElement(By.Name("q")).SendKeys("Stack Overflow”);
只要您使用的是3.14或更高版本的.NET绑定,该代码或类似的代码就应该可以工作 我终于找到了这个问题的答案。这里的堆栈溢出页面在找到这个问题的答案方面帮助很大: 以下是我为headless firefox编写的代码,在非默认位置使用firefox时,该代码非常有效:
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace GoogleSearch
{
class LaunchFirefox
{
static void Main(string[] args)
{
//Start Firefox Gecko Driver Service from Custom Location
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(@"C:\FirefoxDriver");
//Force the CMD prompt window to automatically close and suppress diagnostic information
service.HideCommandPromptWindow = true;
service.SuppressInitialDiagnosticInformation = true;
//Initialize Firefox Options class
FirefoxOptions options = new FirefoxOptions();
//Set Custom Firefox Options
options.BrowserExecutableLocation = @"C:\Mozilla Firefox\Firefox.exe";
//options.AddArgument("--headless");
//Start Firefox Driver with service, options, and timespan arguments
FirefoxDriver driver = new FirefoxDriver(service, options, TimeSpan.FromMinutes(1));
//Navigate to the Google website
driver.Navigate().GoToUrl("https://www.google.com");
//Automate custom Google Search Submission
driver.FindElement(By.Name("q")).SendKeys("Stack Overflow");
}
}
}
希望这能帮助其他人使用C代码并尝试运行headless firefox
祝福,
赛斯感谢您的回复。但是,有了这个代码和类似的代码,每当我尝试向FirefoxDriver行添加选项时,我在Visual Studio中都会遇到此错误:错误CS1503参数1:无法从“OpenQA.Selenium.Firefox.FirefoxDriverService”转换为“OpenQA.Selenium.Firefox.FirefoxBinary”,您知道如何解决此错误吗?能否验证正在使用的.NET绑定的版本?如果您使用的是3.14,那么至少构造函数应该将其作为有效的签名。在3.13之前,以
FirefoxDriverService
和FirefoxOptions
作为参数的构造函数不存在。对于此解决方案,我在Visual Studio中的目标.NET framework是4.6.1。您是否有时间编写和测试几行既使用自定义firefox位置又运行headless firefox的代码?我可以很容易地理解代码的其余部分。