C# Selenium WebDriver FireFox配置文件 - 使用带身份验证的代理

9

如果您在下面的代码中设置代理服务器参数,如果您的代理服务器需要身份验证,那么FireFox将带来身份验证对话框,基本上您无法自动填写它。

那么有没有办法设置用户名密码

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,则会出现无效的错误。因此,我想知道是否有实现这一点的方法。如果您需要帮助,请告诉我们。

我还没有找到答案,目前我只是在我的代理服务器上禁用了身份验证,并允许通过IP范围进行访问,所以现在这样做可以解决问题。 - Tim
我需要什么参考资料来解决ProfilesIni的错误,提示信息为The type or namespace name 'ProfilesIni' could not be found - Nick Kahn
5个回答

2
您可以编写自己的 Firefox 扩展程序来代理,并从 Selenium 启动。您需要编写 2 个文件并打包它。 background.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>"]});

manifest.json

{
  "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模式打包成zip归档文件,并在结尾处添加.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);

1
        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))

0

使用MS UI自动化而不是AutoIt完成了它:

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实现


0
你可以创建一个配置文件并将认证数据保存在其中。 如果你的配置文件名为“webdriver”,你可以在初始化代码中选择它:
ProfilesIni allProfiles = new ProfilesIni(); 
FirefoxProfile profile = allProfiles.getProfile("WebDriver"); 
profile.setPreferences("foo.bar",23);
WebDriver driver = new FirefoxDriver(profile);

那非常有趣,我应该尝试并回报是否有效。 - Tim
太好了!请保持与我沟通 :) - Santiago Hernandez

0

要停止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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接