此示例Python使用Selenium调用Chrome浏览器并通过代理进行自动化测试。
from selenium import webdriverfrom selenium.webdriver.chrome.service import ServicetargetURL = "http://myip.ipip.net" #访问的目标站点proxyAddr = "您的代理IP:端口号" if __name__ == '__main__': browser_location = r".\Chrome\chrome.exe" #指定浏览器路径位置 driver_location = r".\Chrome\chromedriver.exe" #指定Driver路径位置 option = webdriver.ChromeOptions() option.binary_location = browser_location #设置浏览器位置 option.add_argument("--start-maximized") #窗口最大化运行 option.add_argument('--proxy-server=%(server)s' % {"server": proxyAddr}) driver = webdriver.Chrome(service=Service(driver_location), options=option) driver.get(targetURL) print(driver.page_source)