Setting a proxy for Chrome Driver in Selenium
Asked Answered
M

3

23

I am using Selenium Webdriver using C# for Automation in Chrome browser. I need to check if my webpage is blocked 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 someone 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. Your system is currently 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 ensure 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*

Mcnew answered 1/1, 2015 at 11:23 Comment(1)
If you could see XXX.XXX.XXX.XXX set in Chrome proxy, then it's no problem with Selenium; it's because XXX.XXX.XXX.XXX is not up or refuse your connection. You should ensure XXX.XXX.XXX.XXX works first.Lao
A
23

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);
Asta answered 3/2, 2016 at 3:9 Comment(7)
@Toolkit you can try using the SocksUserName and SocksPassword propertiesAsta
@BronDavies, I've tried to use this properties, but still I have popup with asking to enter login and pass in chromeDriver. Does solution exist already?Unlearned
@Unlearned If you could ask a new question and paste your code, I can see what might be missingAsta
Why is the HttpProxy attribute equals to nothing?Mop
@Mop proxy.HttpProxy = proxy.SslProxy = "127.0.0.1:3330"; is equivalent to proxy.HttpProxy = "127.0.0.1:3330"; proxy.SslProxy = "127.0.0.1:3330";Asta
I know it's been a while, but no proxy detectedFlamboyant
@Flamboyant Make sure you're using the right port for the proxy. 3330 is just an exampleAsta
B
9

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:[email protected]:8080");
Bourges answered 2/1, 2015 at 10:19 Comment(1)
"--proxy-server=socks5://" add "socks5://" after for socks 5 proxiesRudin
P
7

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/");
Potsdam answered 24/6, 2018 at 12:3 Comment(4)
What to do if browser in '--headless' mode, in this way extensions doesn't work...Unlearned
Refer This Url >>>> #45372566Potsdam
Where do i find the File ProxyChanger.crx ?Hypoderm
I see some references to proxy but if you want to use Selenium to submit a URL and track all outgoing URLs being called during the generation of the page that show in the Network tab on dev tools in Chrome, how do you do that? I assume for starters, one thing most don't mention is you need an actual proxy server setup for the initial code to work?Vive

© 2022 - 2024 — McMap. All rights reserved.