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.

Basic Setup Example

Code Implementation with proper imports and corrected syntax ?

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# proxy server definition
proxy_ip = "128.21.0.0:8080"

# configure ChromeOptions class
chrome_options = Options()

# proxy parameter to options
chrome_options.add_argument(f'--proxy-server={proxy_ip}')

# options to Chrome()
driver = webdriver.Chrome(options=chrome_options)
driver.implicitly_wait(0.6)
driver.get("https://www.tutorialspoint.com/index.htm")

# Close the driver
driver.quit()

Location Verification Example

To check if a search field has the present user address we shall add the below code snippet ?

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

def check_location():
    proxy_ip = "128.21.0.0:8080"
    
    chrome_options = Options()
    chrome_options.add_argument(f'--proxy-server={proxy_ip}')
    
    driver = webdriver.Chrome(options=chrome_options)
    driver.get("https://example.com")
    
    # Find location element using modern selector
    location_element = driver.find_element(By.ID, 'loc')
    
    # Check location with assertion
    assert 'India' == location_element.text
    print("Location verification passed")
    
    driver.quit()

check_location()

Multiple Proxy Testing

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

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

def test_with_proxy(proxy_address, expected_location):
    """Test website with different proxy locations"""
    
    chrome_options = Options()
    chrome_options.add_argument(f'--proxy-server={proxy_address}')
    
    driver = webdriver.Chrome(options=chrome_options)
    
    try:
        driver.get("https://example.com")
        location_element = driver.find_element(By.ID, 'location')
        
        if location_element.text == expected_location:
            print(f"Test passed for {expected_location}")
        else:
            print(f"Test failed. Expected: {expected_location}, Got: {location_element.text}")
            
    finally:
        driver.quit()

# Test multiple locations
proxies = [
    ("128.21.0.0:8080", "India"),
    ("192.168.1.100:8080", "USA"),
    ("172.16.0.1:8080", "UK")
]

for proxy, location in proxies:
    test_with_proxy(proxy, location)

Key Points

  • Use Options() instead of deprecated ChromeOptions()

  • Use By.ID instead of deprecated find_element_by_xpath()

  • Always close the driver with driver.quit()

  • Use try-finally blocks for proper cleanup

  • Test with valid proxy servers that allow your requests

Conclusion

Setting up Selenium with a proxy allows you to test location-specific features of your web application. Use the Options() class to configure proxy settings and always ensure proper driver cleanup after testing.

Updated on: 2026-03-25T14:06:08+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements