Setting a proxy for Chrome Driver in Selenium

2020-02-05 03:25发布

问题:

I am using Selenium Webdriver using C# for Automation in Chrome browser. I need to check if my webpage is bloced in Some regions(some ip ranges). So I have to set a proxy in my Chrome browser . I tried the below code. The proxy is being set but I get an error. Could some one help me.

        ChromeOptions options = new ChromeOptions();

        options.AddArguments("--proxy-server=XXX.XXX.XXX.XXX");

        IWebDriver Driver = new ChromeDriver(options);

        Driver.Navigate().GoToUrl("myUrlGoesHere");

When I run this code, I get the following message in my Chrome browser: I tried to enable the Proxy option, but the ' Change proxy settings' option is disabled.

*Unable to connect to the proxy server

A proxy server is a server that acts as an intermediary between your computer and other servers. Right now, your system is configured to use a proxy, but Google Chrome can't connect to it. If you use a proxy server... Check your proxy settings or contact your network administrator to make sure the proxy server is working. If you don't believe you should be using a proxy server: Go to the Chrome menu > Settings > Show advanced settings... > Change proxy settings... > LAN Settings and deselect "Use a proxy server for your LAN". Error code: ERR_PROXY_CONNECTION_FAILED*

回答1:

I'm using the nuget packages for Selenium 2.50.1 with this:

ChromeOptions options = new ChromeOptions();
proxy = new Proxy();
proxy.Kind = ProxyKind.Manual;
proxy.IsAutoDetect = false;
proxy.HttpProxy =
proxy.SslProxy = "127.0.0.1:3330";
options.Proxy = proxy;
options.AddArgument("ignore-certificate-errors");
var chromedriver = new ChromeDriver(options);


回答2:

If your proxy requires user log in, you can set the proxy with login user/password details as below:

options.AddArguments("--proxy-server=http://user:password@yourProxyServer.com:8080");


回答3:

Please Following code, this will help you to change the proxy

First create chrome extension and paste the following java script code.

Java Script Code

var Global = {
    currentProxyAouth: {
        username: '',
        password: ''
    }
}

var userString = navigator.userAgent.split('$PC$');
if (userString.length > 1) {
    var credential = userString[1];
    var userInfo = credential.split(':');
    if (userInfo.length > 1) {
        Global.currentProxyAouth = {
            username: userInfo[0],
            password: userInfo[1]
        }
    }
}

chrome.webRequest.onAuthRequired.addListener(
    function(details, callbackFn) {
        console.log('onAuthRequired >>>: ', details, callbackFn);
        callbackFn({
            authCredentials: Global.currentProxyAouth
        });
    }, {
        urls: ["<all_urls>"]
    }, ["asyncBlocking"]);


chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        console.log('Background recieved a message: ', request);

        POPUP_PARAMS = {};
        if (request.command && requestHandler[request.command])
            requestHandler[request.command](request);
    }
);

C# Code

    var cService = ChromeDriverService.CreateDefaultService();
    cService.HideCommandPromptWindow = true;

    var options = new ChromeOptions();

    options.AddArguments("--proxy-server=" + "<< IP Address >>" + ":" + "<< Port Number >>");
    options.AddExtension(@"C:\My Folder\ProxyChanger.crx");

    options.Proxy = null;

    string userAgent = "<< User Agent Text >>";

    options.AddArgument($"--user-agent={userAgent}$PC${"<< User Name >>" + ":" + "<< Password >>"}");

    IWebDriver _webDriver = new ChromeDriver(cService, options);

    _webDriver.Navigate().GoToUrl("https://whatismyipaddress.com/");