how to use chomedriver with a proxy for selenium webdriver?

  • Last Update :
  • Techknowledgy :
  • Import Selenium WebDriver from the package
  • Define the proxy server (IP:PORT)
  • Set ChromeOptions()
  • Add the proxy server argument to the options
  • Add the options to the Chrome() instance
from selenium
import webdriver
PROXY = "11.456.448.110:8080"
chrome_options = WebDriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)
chrome = webdriver.Chrome(chrome_options = chrome_options)
chrome.get("https://www.google.com")

Use this Chrome WebDriver instance to execute tests that incorporate the proxy server. For example, the following code snippet tests to ensure that a search field shows the user’s current city as the default location.

def testUserLocationZurich(self):
   self.chrome.get(self.url)
search = self.chrome.find_element_by_id('user-city')
self.assertIn('Zurich', search.text)

Background.js

var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: "YOUR_PROXY_ADDRESS",
port: parseInt(PROXY_PORT)
},
bypassList: ["foobar.com"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "PROXY_USERNAME",
password: "PROXY_PASSWORD"
}
};
}

chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);

The Chrome extension can be added to Selenium using the add_extension method:

from selenium
import webdriver
from selenium.webdriver.chrome.options
import Options
chrome_options = Options()
chrome_options.add_extension("proxy.zip")
driver = webdriver.Chrome(executable_path = 'chromedriver.exe', chrome_options = chrome_options)
driver.get("http://google.com")
driver.close()

Suggestion : 2

To set up proxy authentication we will generate a special file and upload it to chromedriver dynamically using the following code below. The example is given for BotProxy rotating proxy server, but you can substitute PROXY_HOST and other constants with your values. This code configures selenium with chromedriver to use HTTP proxy that requires authentication with user/password pair.,Function get_chromedriver returns configured selenium webdriver that you can use in your application. This code is tested and works just fine with BotProxy HTTP Rotating Proxy.,It works fine unless proxy requires authentication. if the proxy requires you to login with a username and password it will not work. In this case you have to use more tricky solution that is explained below. By the way if you are going to use BotProxy rotating proxy you can whitelist your server IP address and botproxy will not require HTTP auth to work.,If you need to use proxy with python and Selenium library with chromedriver you usually use the following code:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % hostname + ":" + port)
driver = webdriver.Chrome(chrome_options = chrome_options)
import os
import zipfile

from selenium import webdriver

PROXY_HOST = 'x.botproxy.net'  # rotating proxy
PROXY_PORT = 8080
PROXY_USER = 'proxy-user'
PROXY_PASS = 'proxy-password'


manifest_json = """
{
    "version": "1.0.0",
    "manifest_version": 2,
    "name": "Chrome Proxy",
    "permissions": [
        "proxy",
        "tabs",
        "unlimitedStorage",
        "storage",
        "<all_urls>",
        "webRequest",
        "webRequestBlocking"
    ],
    "background": {
        "scripts": ["background.js"]
    },
    "minimum_chrome_version":"22.0.0"
}
"""

background_js = """
var config = {
        mode: "fixed_servers",
        rules: {
          singleProxy: {
            scheme: "http",
            host: "%s",
            port: parseInt(%s)
          },
          bypassList: ["localhost"]
        }
      };

chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

function callbackFn(details) {
    return {
        authCredentials: {
            username: "%s",
            password: "%s"
        }
    };
}

chrome.webRequest.onAuthRequired.addListener(
            callbackFn,
            {urls: ["<all_urls>"]},
            ['blocking']
);
""" % (PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS)


def get_chromedriver(use_proxy=False, user_agent=None):
    path = os.path.dirname(os.path.abspath(__file__))
    chrome_options = webdriver.ChromeOptions()
    if use_proxy:
        pluginfile = 'proxy_auth_plugin.zip'

        with zipfile.ZipFile(pluginfile, 'w') as zp:
            zp.writestr("manifest.json", manifest_json)
            zp.writestr("background.js", background_js)
        chrome_options.add_extension(pluginfile)
    if user_agent:
        chrome_options.add_argument('--user-agent=%s' % user_agent)
    driver = webdriver.Chrome(
        os.path.join(path, 'chromedriver'),
        chrome_options=chrome_options)
    return driver

def main():
    driver = get_chromedriver(use_proxy=True)
    #driver.get('https://www.google.com/search?q=my+ip+address')
    driver.get('https://httpbin.org/ip')

if __name__ == '__main__':
    main()

Suggestion : 3

Selenium WebDriver provides a way to proxy settings:,WebDriver provides capabilities that each remote end will/should support the implementation. The following capabilities are supported by WebDriver:,When set to none Selenium WebDriver only waits until the initial page is downloaded.,This will make Selenium WebDriver to wait for the entire page is loaded. When set to normal, Selenium WebDriver waits until the load event fire is returned.

import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.chrome.ChromeDriver;

public class pageLoadStrategy {
   public static void main(String[] args) {
      ChromeOptions chromeOptions = new ChromeOptions();
      chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
      WebDriver driver = new ChromeDriver(chromeOptions);
      try {
         // Navigate to Url
         driver.get("https://google.com");
      } finally {
         driver.quit();
      }
   }
}
from selenium
import webdriver
from selenium.webdriver.chrome.options
import Options
options = Options()
options.page_load_strategy = 'normal'
driver = webdriver.Chrome(options = options)
driver.get("http://www.google.com")
driver.quit()
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace pageLoadStrategy {
   class pageLoadStrategy {
      public static void Main(string[] args) {
         var chromeOptions = new ChromeOptions();
         chromeOptions.PageLoadStrategy = PageLoadStrategy.Normal;
         IWebDriver driver = new ChromeDriver(chromeOptions);
         try {
            driver.Navigate().GoToUrl("https://example.com");
         } finally {
            driver.Quit();
         }
      }
   }
}
require 'selenium-webdriver'
caps = Selenium::WebDriver::Remote::Capabilities.chrome
caps.page_load_strategy = 'normal'

driver = Selenium::WebDriver.for: chrome,: desired_capabilities => caps
driver.get('https://www.google.com')
        it('Navigate using normal page loading strategy', async function() {
                    let caps = new Capabilities();
                    caps.setPageLoadStrategy("normal");
                    let driver = await env.builder().withCapabilities(caps).build();

                    await driver.get('https://www.google.com');

                    driver.quit();
import org.openqa.selenium.PageLoadStrategy
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions

fun main() {
   val chromeOptions = ChromeOptions()
   chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL)
   val driver = ChromeDriver(chromeOptions)
   try {
      driver.get("https://www.google.com")
   } finally {
      driver.quit()
   }
}

Suggestion : 4

Best Selenium code snippet using org.openqa.selenium.Proxy.setNoProxy,Execute automation tests with setNoProxy on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications., Automation Testing Advisor,Automation Testing Advisor

1 package br.gov.caixa.siemp.utils;
2
3
import org.openqa.selenium.Proxy;
4
import org.openqa.selenium.WebDriver;
5
import org.openqa.selenium.chrome.ChromeDriver;
6
import org.openqa.selenium.firefox.FirefoxDriver;
7
import org.openqa.selenium.ie.InternetExplorerDriver;
8
import org.openqa.selenium.remote.CapabilityType;
9
import org.openqa.selenium.remote.DesiredCapabilities;
10
11
import br.gov.caixa.siemp.utils.Properties.DriverType;
12
13 public class DriverFactory {
   14
   15 public static WebDriver driver;
   16 static String path = System.getProperty("user.dir");
   17
   18 public static void quitDriver(WebDriver driver) {
      19 driver.quit();
      20 driver = null;
      21
   }
   22
   23 public static WebDriver getDriver() {
      24
      return driver;
      25
   }
   26
   27 @SuppressWarnings("deprecation")
   28 public WebDriver getDriver(DriverType Browser) {
      29
      switch (Browser) {
         30
         case CHROME:
            31 System.setProperty("webdriver.chrome.driver", path + "\\resources\\chromedriver.exe");
            32
            /*
33			 * Proxy pc = new Proxy(); pc.setNoProxy(""); DesiredCapabilities capc = new
34			 * DesiredCapabilities(); capc.setCapability(CapabilityType.PROXY, pc);
35			 */
            36
            37
            return driver = new ChromeDriver();
            38
         case FIREFOX:
            39 System.setProperty("webdriver.gecko.driver", path + "\\resources\\geckodriver.exe");
            40 Proxy pf = new Proxy();
            41 pf.setNoProxy("");
            42 DesiredCapabilities capf = new DesiredCapabilities();
            43 capf.setAcceptInsecureCerts(true);
            44 capf.setCapability(CapabilityType.PROXY, pf);
            45
            return driver = new FirefoxDriver(capf);
            46
         case IE:
            47 System.setProperty("webdriver.ie.driver", path + "\\resources\\IEDriverServer.exe");
            48
            49
            return driver = new InternetExplorerDriver();
            50
         default:
            51 System.out.println("\nDriver n�o informado ou inexistente!\n");
            52
            break;
            53
      }
      54
      return DriverFactory.driver;
      55
   }
   56
   57
}
58
2._
package br.gov.caixa.siemp.utils;

import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import br.gov.caixa.siemp.utils.Properties.DriverType;

public class DriverFactory {

   public static WebDriver driver;
   static String path = System.getProperty("user.dir");

   public static void quitDriver(WebDriver driver) {
      driver.quit();
      driver = null;
   }

   public static WebDriver getDriver() {
      return driver;
   }

   @SuppressWarnings("deprecation")
   public WebDriver getDriver(DriverType Browser) {
      switch (Browser) {
         case CHROME:
            System.setProperty("webdriver.chrome.driver", path + "\\resources\\chromedriver.exe");
            /*
             * Proxy pc = new Proxy(); pc.setNoProxy(""); DesiredCapabilities capc = new
             * DesiredCapabilities(); capc.setCapability(CapabilityType.PROXY, pc);
             */

            return driver = new ChromeDriver();
         case FIREFOX:
            System.setProperty("webdriver.gecko.driver", path + "\\resources\\geckodriver.exe");
            Proxy pf = new Proxy();
            pf.setNoProxy("");
            DesiredCapabilities capf = new DesiredCapabilities();
            capf.setAcceptInsecureCerts(true);
            capf.setCapability(CapabilityType.PROXY, pf);
            return driver = new FirefoxDriver(capf);
         case IE:
            System.setProperty("webdriver.ie.driver", path + "\\resources\\IEDriverServer.exe");

            return driver = new InternetExplorerDriver();
         default:
            System.out.println("\nDriver n�o informado ou inexistente!\n");
            break;
      }
      return DriverFactory.driver;
   }

}
package br.gov.caixa.siemp.utils;

import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import br.gov.caixa.siemp.utils.Properties.DriverType;

public class DriverFactory {

   public static WebDriver driver;
   static String path = System.getProperty("user.dir");

   public static void quitDriver(WebDriver driver) {
      driver.quit();
      driver = null;
   }

   public static WebDriver getDriver() {
      return driver;
   }

   @SuppressWarnings("deprecation")
   public WebDriver getDriver(DriverType Browser) {
      switch (Browser) {
         case CHROME:
            System.setProperty("webdriver.chrome.driver", path + "\\resources\\chromedriver.exe");
            /*
             * Proxy pc = new Proxy(); pc.setNoProxy(""); DesiredCapabilities capc = new
             * DesiredCapabilities(); capc.setCapability(CapabilityType.PROXY, pc);
             */

            return driver = new ChromeDriver();
         case FIREFOX:
            System.setProperty("webdriver.gecko.driver", path + "\\resources\\geckodriver.exe");
            Proxy pf = new Proxy();
            pf.setNoProxy("");
            DesiredCapabilities capf = new DesiredCapabilities();
            capf.setAcceptInsecureCerts(true);
            capf.setCapability(CapabilityType.PROXY, pf);
            return driver = new FirefoxDriver(capf);
         case IE:
            System.setProperty("webdriver.ie.driver", path + "\\resources\\IEDriverServer.exe");

            return driver = new InternetExplorerDriver();
         default:
            System.out.println("\nDriver n�o informado ou inexistente!\n");
            break;
      }
      return DriverFactory.driver;
   }

}