Hi Guys,
Ever needed to change your IP address or access region-restricted websites while using Selenium? or prevent your ip from being blocked from scraping from sites? Find out how to switch between proxies to on every call made with selenium
Step 1
firstly lets get our proxies from proxyscrape. they provide up to 100 free shared proxies you can use for your testing without paying for anything.
Step 2
Once you’ve signed up, head over to ‘IP Authentication’ in the side menu. Click on ‘Add New IP’ to register your current IP and authenticate it for the proxies. Not sure what your current IP is? Check it out on ipfy.com – it’s that simple!
Step 3
Navigate to ” Proxy List” on the side menu. Click “Download” on the table having the list of your proxies, you can copy them. Now lets move to coding
Step 4
lets have a function to randomly take each proxy on each call
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType
proxies = [
#list of proxies goes here
]
#this function is to get random proxies from the list
def random_proxy():
random_proxy = random.choice(proxies)
return random_proxy
service = Service(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()))
# Configure Chrome options with the proxy
chrome_options = webdriver.ChromeOptions()
proxy = random_proxy()
chrome_options.add_argument('--proxy-server=%s' % proxy)
# Initialize the Chrome driver with the configured options
driver = webdriver.Chrome(
service=service,
options=chrome_options)
# Example: Open a webpage
driver.get("https://example.com")
#to see if it works you can open ipfy.com to see if it uses the proxy you added
driver.get("https://api.ipify.org/")
#this will print out the ip that is used
Thats all you done!, Thank you