小编典典

Selenium C#中的无头Firefox

selenium

我想无头运行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应该无头运行,但会在正常窗口中打开。


更新 我将firefox更新为56.0.1

现在我得到了另一个错误:

WebDriver.dll中发生了类型为’OpenQA.Selenium.WebDriverException’的未处理异常

附加信息:预期的浏览器二进制位置,但在默认位置找不到二进制文件,未提供’moz:firefoxOptions.binary’功能,并且命令行上未设置二进制标志


阅读 649

收藏
2020-06-26

共1个答案

小编典典

Windows和Mac OS上的版本56支持Firefox中的Headless模式。确保您安装了正确的版本。

https://developer.mozilla.org/zh-
CN/Firefox/Headless_mode#Browser_support

使用 Firefox v56.0.1Selenium.WebDriver v3.6.0geckodriver
v0.19.0(x64),
这对我来说是正确的。

关于错误:

WebDriver.dll中发生了类型为’OpenQA.Selenium.WebDriverException’的未处理异常

确保您使用的是正确版本的geckodriver。我怀疑您在机器x32上使用构建x64,请获取x64构建。

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

2020-06-26