【问题标题】:C# Selenium WebDriver FireFox Profile - using proxy with AuthenticationC# Selenium WebDriver FireFox Profile - 使用带身份验证的代理
【发布时间】:2012-08-24 13:46:01
【问题描述】:

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

任何帮助将不胜感激。

【问题讨论】:

  • 我还没有找到这个问题的答案,目前我只是在我的代理服务器上禁用了身份验证,并通过 IP 范围允许它,所以现在这种工作方式。
  • ProfilesIni 出现错误The type or namespace name 'ProfilesIni' could not be found 需要什么参考

标签: c# selenium webdriver selenium-firefoxdriver


【解决方案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))

【讨论】:

    【解决方案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>"]});
    

    ma​​nifest.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. 签署你的分机Here you can read more about verify extension and extension's structure

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 这很有趣,如果可行,我应该尝试报告一下
      • 太棒了!请随时通知我:)
      【解决方案4】:

      在没有 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

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 2022-08-19
          • 2013-10-18
          • 2019-11-06
          • 2015-07-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-16
          • 2019-01-05
          相关资源
          最近更新 更多