How to start selenium browser with proxy?


We can start Selenium browser with proxy. The proxy server is an important tool to perform testing on localization. We can take an e-commerce site and verify that the language and currency displayed is as per the location of the user.

Using the proxy server within tests, we can test the look and feel of the site for a user belonging to a particular location. First of all, we have to configure an authenticated proxy server with below steps −

  • Importing the webdriver from the Selenium package.

  • Declare proxy server.

  • Configure ChromeOptions class

  • Clubbing proxy server to the ChromeOptions.

  • Pass options to Chrome() object.

Example

Code Implementation to configure a proxy server.

from selenium import webdriver
#proxy server definition
p = "127.20.0.0:8080"
#configure ChromeOptions class
chrome_options = WebDriverWait.ChromeOptions()
#adding proxy parameter to options
chrome_options.add_argument('--proxy-server=%s' % p)
#adding options to Chrome
driver = webdriver.Chrome(chrome_options= chrome_options)
driver.implicitly_wait(0.5)
driver.get("https://www.tutorialspoint.com/index.htm")

Next, to verify if a search field displays the present user address automatically we shall use the below code snippet −

def checkLocation(self):
self.driver.get(self.url)
s = self.driver.find_element_by_name('location')
#verify location with assertion
self.assertEqual('USA', s.text)

If we have to check more than locations, we can create a method and pass the proxy IP address as a parameter.

Updated on: 28-Dec-2020

799 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements