selenium连接远程chrome浏览器

发布于:2024-08-31 ⋅ 阅读:(234) ⋅ 点赞:(0)

连接本地浏览器

  1. 命令行启动浏览器

    chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenium\ChromeProfile"
    
  2. selenium连接

    from selenium import webdriver
    
    options = webdriver.ChromeOptions()
    options.debugger_address = "127.0.0.1:9222"
    service = webdriver.ChromeService(
        executable_path=r'C:\Users\Administrator\.cache\selenium\chromedriver\win64\128.0.6613.84\chromedriver.exe')
    driver = webdriver.Chrome(service=service, options=options)
    driver.get("https://www.baidu.com")
    # 其他操作
    

连接远程浏览器

  1. 在远程机器命令行启动浏览器

    chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenium\ChromeProfile"
    
  2. 在远程机器安装socat-windows,使用9223端口代理本地9222端口

    • 下载地址:https://github.com/StudioEtrange/socat-windows

    • 启动代理

      socat.exe TCP-LISTEN:9223,fork TCP:localhost:9222
      
  3. 本地selenium连接

    from selenium import webdriver
    
    options = webdriver.ChromeOptions()
    options.debugger_address = "192.168.100.12:9223"
    service = webdriver.ChromeService(
        executable_path=r'C:\Users\Administrator\.cache\selenium\chromedriver\win64\128.0.6613.84\chromedriver.exe')
    driver = webdriver.Chrome(service=service, options=options)
    driver.get("https://www.baidu.com")
    # 其他操作
    

使用selenium-grid

  1. 下载jar包

    • 下载地址:https://github.com/SeleniumHQ/selenium/releases
    • 参考文档:https://www.selenium.dev/zh-cn/documentation/grid/getting_started/
  2. 命令行启动

    java -jar selenium-server-4.23.0.jar standalone --username test --password 123456 --port 8899 --max-sessions 2 --session-timeout 30
    
  3. 启动远程浏览器测试

    from selenium import webdriver
    
    options = webdriver.ChromeOptions()
    driver = webdriver.Remote('http://test:123456@192.168.100.156:8899', options=options)
    driver.maximize_window()
    # 需要关闭浏览器
    driver.quit()
    

使用chrome-proxy

  1. 安装: pip install chrome-proxy -U

  2. 命令行启动代理服务器:chrome_proxy

  3. 本地selenium连接

    from selenium import webdriver
    
    options = webdriver.ChromeOptions()
    options.debugger_address = "192.168.100.12:9223"
    service = webdriver.ChromeService(
        executable_path=r'C:\Users\Administrator\.cache\selenium\chromedriver\win64\128.0.6613.84\chromedriver.exe')
    driver = webdriver.Chrome(service=service, options=options)
    driver.get("https://www.baidu.com")
    # 其他操作
    

网站公告

今日签到

点亮在社区的每一天
去签到