分享页面

Python自动化测试Selenium+chrome连接代理ip(白名单模式)

此示例Python使用Selenium调用Chrome浏览器并通过代理进行自动化测试。

请注意:目前最高支持到Chrome 129.0.6614.3,后续版本暂不支持

  • 代码示例
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

targetURL = "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)
  • 运行结果

img

本文导读