将自定义Firefox二进制位置与无头Firefox选项一起使用

时间:2018-08-13 22:28:30

标签: c# selenium-webdriver geckodriver

我正在尝试编写C#代码,该代码将启动Mozilla Firefox,浏览到一个网站并自动执行表单输入。我可以使它正常运行而不会变得无头,但是现在我希望将代码转换为运行无头Firefox浏览器。

如果通过NuGet安装了最新版本的Selenium和Firefox驱动程序,并且最新版本的geckodriver也位于相应的文件夹位置,则以下代码将起作用。

要使此代码打开无头的Mozilla Firefox,需要做些什么?

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”

任何帮助将不胜感激。

我正在运行以下软件:

  • Windows 7
  • Visual Studio Community Edition 2017
  • Mozilla Firefox 61.0.1
  • Gecko驱动程序0.21.0

2 个答案:

答案 0 :(得分:0)

由于以无头模式使用Firefox是通过向Firefox命令行传递--headless选项来完成的,因此所需的代码类似于以下内容:

// 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绑定,该代码或类似的代码都应该可以使用。

答案 1 :(得分:0)

我终于找到了这个问题的答案。此处的堆栈溢出页面有助于找到该问题的答案:

Setting BrowserExecutableLocation in FirefoxOptions in Selenium doesn't prevent an "Unable to find a matching set of capabilities" error

这是我的无头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#代码帮助其他人并尝试运行无头firefox。

祝福

塞思

相关问题