Headless Firefox in Selenium C#

家住魔仙堡 提交于 2019-12-21 09:17:39

问题


I want to run firefox headless.

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

Problem is I know how to do it in chrome but not in Firefox.

My code:

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);
    }
}
}

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.


Update I updated firefox to 56.0.1

Now I get a different error:

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


回答1:


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/en-US/Firefox/Headless_mode#Browser_support

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

Regarding the error:

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

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



来源:https://stackoverflow.com/questions/46848615/headless-firefox-in-selenium-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!