Selenium C中的无头Firefox#

问题描述:

我想运行firefox无头。Selenium C中的无头Firefox#

不隐藏浏览器窗口或在虚拟桌面中打开它,Firefox使用“-headless”标志支持无头模式。

问题是我知道如何使用chrome而不是Firefox。

我的代码:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using OpenQA.Selenium; 
using OpenQA.Selenium.Firefox; 

namespace MyApp { 
public partial class Form1: Form { 
    public Form1() { 
     InitializeComponent(); 
    } 

    private void StartBtn_Click(object sender, EventArgs e) { 

     IWebDriver driver; 
     FirefoxOptions options = new FirefoxOptions(); 
     options.AddArguments("--headless"); 
     driver = new FirefoxDriver(options); 
    } 
} 
} 

我的WinForm应用程序只与名称StartBtn按钮。 单击按钮Firefox应该运行无头,但它会在一个普通窗口中打开。


更新 我更新火狐56.0.1

现在,我得到一个不同的错误:

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

Additional information: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line

在Firefox

无头模式从56版本支持Windows和Mac OS 。确保安装了正确的版本。

https://developer.mozilla.org/en-US/Firefox/Headless_mode#Browser_support

随着火狐v56.0.1Selenium.WebDriver V3.6.0geckodriver v0.19.0(64)这正常工作对我来说。

关于错误:

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

确保你使用的geckodriver正确的版本。我怀疑你在x64机器上使用x32版本,得到x64版本。

https://github.com/mozilla/geckodriver/releases

+0

它显示了一个例外,当我使用'--headless'。 **所有参数必须以两个破折号(' - ')开头;参数'-headless'没有。** – Raven

+0

我更新了Firefox的最新版本,我现在得到了一个不同的错误。请检查我更新的问题。 – Raven

+0

@Raven你正在使用哪个版本的'geckodriver'?我想你可能在64位系统上使用32位。尝试x64版本:https://github.com/mozilla/geckodriver/releases – Equalsk