2017-10-20 84 views
2

我想運行firefox無頭。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應該運行無頭,但它會在一個普通窗口中打開。


更新 我更新火狐56.0.1

現在,我得到一個不同的錯誤:

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

回答

0
在Firefox

無頭模式從56版本支持Windows和Mac OS 。確保安裝了正確的版本。

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

隨着火狐v56.0.1Selenium.WebDriver V3.6.0geckodriver v0.19.0(64)這正常工作對我來說。

關於錯誤:

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

確保你使用的geckodriver正確的版本。我懷疑你在x64機器上使用x32版本,得到x64版本。

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

+0

它顯示了一個例外,當我使用'--headless'。 **所有參數必須以兩個破折號(' - ')開頭;參數'-headless'沒有。** – Raven

+0

我更新了Firefox的最新版本,我現在得到了一個不同的錯誤。請檢查我更新的問題。 – Raven

+0

@Raven你正在使用哪個版本的'geckodriver'?我想你可能在64位系統上使用32位。嘗試x64版本:https://github.com/mozilla/geckodriver/releases – Equalsk

相關問題