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

时间:2014-03-18 10:34:33

标签: selenium proxy

我用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”的驱动程序。我在我的代码中展示了(// LINE 5)。请帮我解决这个问题。

感谢: Ramakrishna K.C

2 个答案:

答案 0 :(得分:0)

您可以使用Kind = ProxyKind.System

创建代理

new Proxy { Kind = ProxyKind.System};

然后更新注册表中的互联网设置

var proxyServer = string.Format("http={0};https={0}", ipAddressAndPort);
        var proxyEnable = enableProxy ? 1 : 0;

        const string subKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Internet 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);
        }

答案 1 :(得分:0)

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