How can I manually set proxy settings in Python Selenium?


We can manually set proxy settings using Selenium webdriver in Python. It is done using the DesiredCapabilities class. We would create an object of this class and apply the add_to_capabilities method to it. Then pass the proxy capabilities as a parameter to this method.

Example

Code Implementation

from selenium import webdriver
from selenium.webdriver.common.proxy import ProxoxyType

#add proxy’s ip and port
p = '<proxy ip, port>'
pxy = Proxy()

#set proxy type
pxy.p_type = ProxyType.MANUAL

#http proxy
pxy.http_pxy = p

#ssl proxy
pxy.ssl_pxy = p

#object of DesiredCapabilities
c = webdriver.DesiredCapabilities.CHROME

#set proxy browser capabilties
pxy.add_to_capabilities(c)

#set chromedriver.exe path
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe", desired_capabilities = c)

#launch URL
driver.get("https://www.tutorialspoint.com/index.htm")

#quit browser
driver.quit()

Updated on: 18-Nov-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements