在同一浏览器中多次配置Selenium代理



我用selenium工具打开了一个浏览器,并通过下面发布的代码向该浏览器应用代理,下面是Firefox

    //LINE 1    FirefoxProfile profile = new FirefoxProfile();
    //LINE 2    profile.setPreference("network.proxy.http", configuration
                 .getProxyConfiguration().getHostname());
    //LINE 3    profile.setPreference("network.proxy.http_port", configuration
                 .getProxyConfiguration().getPort());
    //LINE 4    profile.setPreference("network.proxy.type", configuration
                 .getProxyConfiguration().getType().toInt());

    //LINE 5    return new FirefoxDriver(profile); 

现在,我想为同一个浏览器应用另一个代理配置(因为,如果我使用另一个浏览器,会话将得到更改,所以....我想将我的更改应用到该浏览器本身)。如何将代理配置应用于同一浏览器。当我使用相同的代码时,我必须返回使用"NEW"的驱动程序。我在我的代码中显示(//第5行),请帮助我解决这个问题。

谢谢:室利罗摩克里希纳kc

您可以使用Kind = ProxyKind创建代理。系统

new Proxy { Kind = ProxyKind.System};

然后更新注册表中的internet设置

var proxyServer = string.Format("http={0};https={0}", ipAddressAndPort);
        var proxyEnable = enableProxy ? 1 : 0;
        const string subKeyPath = @"SoftwareMicrosoftWindowsCurrentVersionInternet Settings";
        using (var subKey = Registry.CurrentUser.CreateSubKey(subKeyPath)) 
        {
            if (subKey == null)
            {
                throw new Exception(string.Format("Failed to create or open subKey. SubKeyPath: {0} ", subKeyPath));
            }
            subKey.SetValue("ProxyServer", proxyServer, RegistryValueKind.String);
            subKey.SetValue("ProxyEnable", proxyEnable, RegistryValueKind.DWord);
        }

否则使用firefox配置文件中的PAC文件。http://findproxyforurl.com/

最新更新