Selenium C#中的无头Firefox [英] Headless Firefox in Selenium C#

查看:65
本文介绍了Selenium C#中的无头Firefox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想无头运行Firefox.

I want to run firefox headless.

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

Not hide the browser window or open it in a virtual desktop, Firefox supports headless mode by using "-headless" flag.

问题是我知道如何在chrome中做到这一点,但在Firefox中却不行.

Problem is I know how to do it in chrome but not in 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应该无头运行,但会在正常窗口中打开.

My WinForm application only has a button with name StartBtn. On clicking of the button Firefox should run headless, but it opens in a normal window.

更新 我将firefox更新为56.0.1

Update I updated firefox to 56.0.1

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

Now I get a different error:

类型为'OpenQA.Selenium.WebDriverException'的未处理异常 发生在WebDriver.dll

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

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

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

推荐答案

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

Headless mode in Firefox is supported from version 56 on Windows and Mac OS. Ensure that you have the correct version installed.

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

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

With Firefox v56.0.1, Selenium.WebDriver v3.6.0 and geckodriver v0.19.0 (x64) this works correctly for me.

关于错误:

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

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

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

Ensure you're using the correct version of geckodriver. I suspect you're using the x32 build on an x64 machine, get the x64 build.

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

这篇关于Selenium C#中的无头Firefox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆