9

当您在下面的代码中设置代理服务器参数时,如果您的代理服务器需要身份验证,那么 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 ,则会收到错误,该字符串无效。所以我想知道一定有一种方法可以实现这一目标。

任何帮助,将不胜感激。

4

5 回答 5

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))
于 2014-07-24T14:41:14.607 回答
1

您可以为代理编写自己的 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>"]});

清单.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 模式下以.xpi结尾的压缩存档,如my_proxy_extension.xpi

你有两个选择:

  1. 签署你的扩展 在这里你可以阅读更多关于验证扩展和扩展结构的信息

    或者

  2. 运行无符号。对于这一步:

    • 打开 Firefox 标志about:config并将选项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);
于 2020-05-15T23:00:35.107 回答
0

您可以做的是创建配置文件并将身份验证数据保存在其中。如果您的配置文件名为“webdriver”,您可以在初始化时从代码中选择它:

ProfilesIni allProfiles = new ProfilesIni(); 
FirefoxProfile profile = allProfiles.getProfile("WebDriver"); 
profile.setPreferences("foo.bar",23);
WebDriver driver = new FirefoxDriver(profile);
于 2013-01-12T03:20:21.817 回答
0

在没有 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移动鼠标

于 2015-12-12T16:12:51.380 回答
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);
于 2019-01-05T08:26:29.633 回答