[英]Headless Firefox in 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應該無頭運行,但會在正常窗口中打開。
更新我將firefox更新為56.0.1
現在我得到了另一個錯誤:
WebDriver.dll中發生類型'OpenQA.Selenium.WebDriverException'的未處理異常
附加信息:預期的瀏覽器二進制位置,但在默認位置找不到二進制文件,未提供'moz:firefoxOptions.binary'功能,並且在命令行上未設置二進制標志
Windows和Mac OS上的版本56支持Firefox中的Headless模式。 確保您安裝了正確的版本。
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),這對我來說是正確的。
關於錯誤:
WebDriver.dll中發生類型'OpenQA.Selenium.WebDriverException'的未處理異常
確保您使用的geckodriver
版本正確。 我懷疑您在x64
機器上使用x32
構建,請獲取x64
構建。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.