How to start selenium browser with proxy?

SeleniumAutomation TestingTesting Tools

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.


Code Implementation to configure a proxy server.

from selenium import webdriver
#proxy server definition
p = ""
#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)

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

def checkLocation(self):
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.

Published on 28-Dec-2020 17:48:22