Running Selenium Webdriver with a proxy in Python.


We can run a proxy with Selenium webdriver in Python. A proxy is an essential component to do localization testing. We can take an e-commerce application and check if the language and currency visible is as per the user location.

With the help of proxy within tests, we can verify if the website user interface matches with the location. We have to SET a proxy with below steps −

  • Import webdriver from Selenium package.

  • Define proxy server address.

  • Create an object of ChromeOptions class

  • Communication of proxy with ChromeOptions.

  • Summing options to Chrome() object.

Example

Code Implementation.

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

Then, to check if a search field has the present user address we shall add the below code snippet −

def checkL(self):
self.driver.get(self.url)
st = self.driver.find_element_by_xpath('#loc')
#check location with assertion
self.assertEqual('India', st.text)

If we have to verify more than locations, we can create a method and pass the proxy address as an argument.

Updated on: 28-Dec-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements