【问题标题】:Headless Firefox in Selenium C#Selenium C# 中的无头 Firefox
【发布时间】:2017-10-20 11:58:40
【问题描述】:

我想无头运行 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

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

“OpenQA.Selenium.WebDriverException”类型的未处理异常 发生在 WebDriver.dll 中

附加信息:预期的浏览器二进制位置,但无法 在默认位置查找二进制文件,没有 'moz:firefoxOptions.binary' 提供了功能,并且在命令行上没有设置二进制标志

【问题讨论】:

    标签: c# selenium firefox headless headless-browser


    【解决方案1】:

    在 Windows 和 Mac OS 上从版本 56 开始支持 Firefox 中的无头模式。确保您安装了正确的版本。

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

    使用 Firefox v56.0.1Selenium.WebDriver v3.6.0geckodriver v0.19.0 (x64) 这对我来说可以正常工作。

    关于错误:

    WebDriver.dll 中出现“OpenQA.Selenium.WebDriverException”类型的未处理异常

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

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

    【讨论】:

    • 当我使用--headless 时显示异常。 所有参数必须以两个破折号('--')开头;参数“-headless”没有。
    • 我将 firefox 更新到了最新版本,但现在出现了不同的错误。请检查我更新的问题。
    • @Raven 您使用的是哪个版本的geckodriver?我认为您可能在 64 位系统上使用 32 位。试试 x64 版本:github.com/mozilla/geckodriver/releases
    • 用 64 位替换 32 位 geckodriver 有效。非常感谢!
    猜你喜欢
    • 2019-06-22
    • 1970-01-01
    • 2014-06-17
    • 2018-06-15
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    相关资源
    最近更新 更多