繁体   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