Setting a proxy for Chrome Driver in Selenium
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);
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");
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/");