selenium连接远程chrome浏览器

连接本地浏览器

  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()
    
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值