簡體   English   中英

C# Selenium WebDriver FireFox Profile - 使用代理和身份驗證

[英]C# Selenium WebDriver FireFox Profile - using proxy with Authentication

當您在下面的代碼中設置代理服務器參數時,如果您的代理服務器需要身份驗證,那么 FireFox 將帶來身份驗證對話框,基本上您無法自動填寫它。 那么有沒有辦法設置USERNAMEPASSWORD

FirefoxProfile profile = new FirefoxProfile();
String PROXY = "192.168.1.100:8080";
OpenQA.Selenium.Proxy proxy = new OpenQA.Selenium.Proxy();
proxy.HttpProxy=PROXY;
proxy.FtpProxy=PROXY;
proxy.SslProxy=PROXY;
profile.SetProxyPreferences(proxy);
FirefoxDriver driver = new FirefoxDriver(profile);

如果您嘗試將代理字符串格式化為類似http://username:pass@192.168.1.1:8080格式,您會收到字符串無效的錯誤消息。 所以我想知道必須有一種方法來實現這一目標。

任何幫助,將不勝感激。

        String PROXY = "http://login:pass@proxy:port";
        ChromeOptions options = new ChromeOptions();

        options.AddArguments("user-data-dir=path/in/your/system");

        Proxy proxy = new Proxy();

        proxy.HttpProxy = PROXY;
        proxy.SslProxy  = PROXY;
        proxy.FtpProxy  = PROXY;

        options.Proxy = proxy;

        // Initialize the Chrome Driver
        using (var driver = new ChromeDriver(options))

您可以為代理編寫自己的 firefox 擴展,並從 selenium 啟動。 你需要寫2個文件並打包。

背景.js

var proxy_host = "YOUR_PROXY_HOST";
var proxy_port = YOUR_PROXY_PORT;

var config = {
    mode: "fixed_servers",
    rules: {
      singleProxy: {
        scheme: "http",
        host: proxy_host,
        port: proxy_port
      },
      bypassList: []
    }
 };


function proxyRequest(request_data) {
    return {
        type: "http",
        host: proxy_host, 
        port: proxy_port
    };
}

browser.proxy.settings.set({value: config, scope: "regular"}, function() {;});

function callbackFn(details) {
return {
    authCredentials: {
        username: "YOUR_USERNAME",
        password: "YOUR_PASSWORD"
    }
};
}

browser.webRequest.onAuthRequired.addListener(
        callbackFn,
        {urls: ["<all_urls>"]},
        ['blocking']
);

browser.proxy.onRequest.addListener(proxyRequest, {urls: ["<all_urls>"]});

清單文件

{
  "name": "My Firefox Proxy",
  "version": "1.0.0b",
  "manifest_version": 2,
  "permissions": [
    "browsingData",
    "proxy",
    "storage",
    "tabs",
    "webRequest",
    "webRequestBlocking",
    "downloads",
    "notifications",
    "<all_urls>"
  ],
  "background": {
    "scripts": ["background.js"]
  },
  "browser_specific_settings": {
    "gecko": {
      "id": "myproxy@example.org"
    }
  }
}

接下來,您需要將此文件打包為壓縮文件,以 DEFLATED 模式將.xpi壓縮到結尾,如my_proxy_extension.xpi

你有兩個選擇:

  1. 簽署你的擴展在這里你可以閱讀更多關於驗證擴展和擴展結構的信息

    要么

  2. 運行未簽名。 對於這一步:

    • about:config打開 firefox 標志並將選項xpinstall.signatures.required設置為false

    要么

    • 在以下位置更新 Firefox 配置文件:

      Windows : C:\\Program Files\\Mozilla Firefox\\defaults\\pref\\channel-prefs.js

      Linux :/etc/firefox/syspref.js

    將下一行添加到文件末尾:

    pref("xpinstall.signatures.required",false);

在此步驟之后運行 selenium 並安裝此擴展:

FirefoxProfile profile = new FirefoxProfile();

profile.addExtension(new File("path/to/my_proxy_extension.xpi"));

driver = new FirefoxDriver(profile);

您可以做的是創建一個配置文件並將身份驗證數據保存在其中。 如果您的配置文件名為“webdriver”,您可以在初始化時從您的代碼中選擇它:

ProfilesIni allProfiles = new ProfilesIni(); 
FirefoxProfile profile = allProfiles.getProfile("WebDriver"); 
profile.setPreferences("foo.bar",23);
WebDriver driver = new FirefoxDriver(profile);

在沒有 AutoIt 的情況下使用 MS UI 自動化做到了:

public void AuthInProxyWindow (string login, string pass)
    {
        var proxyWindow = AutomationElement.RootElement
            .FindFirst(TreeScope.Subtree,
                new PropertyCondition(AutomationElement.ClassNameProperty, "MozillaDialogClass"));

        var edits = proxyWindow.FindAll(TreeScope.Subtree,
            new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));

        var unamePoint = edits[1].GetClickablePoint();
        Mouse.MoveTo(new Point((int) unamePoint.X, (int) unamePoint.Y));
        Mouse.Click(MouseButton.Left);

        SendKeys.SendWait(login);
        var pwdPoint = edits[2].GetClickablePoint();
        Mouse.MoveTo(new Point((int) pwdPoint.X, (int) pwdPoint.Y));
        Mouse.Click(MouseButton.Left);
        SendKeys.SendWait(pass);

        Keyboard.Press(Key.Return);
        Logger.Debug("Authefication in Firefox completed succesfully");
    }

鼠標移動由Microsoft.TestApi

要阻止 firefox 向您提供身份驗證彈出窗口,請確保您將代理 URL 設置為在設置階段包含身份驗證詳細信息,如下所示:

var myProxy = user + ":" + pass + "@" + proxyIP + ":" + proxyPORT;
options.SetPreference("network.proxy.type", 1);
options.SetPreference("network.proxy.http", myProxy);
options.SetPreference("network.proxy.http_port", proxyPORT);
options.SetPreference("general.useragent.override", useragent);
driver = new FirefoxDriver(driverService, options);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM