You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium中webdriver.Remote无法运行问题求助

webdriver.Remote无法运行的排查与解决

你本地直接使用webdriver.Chrome初始化浏览器可以正常访问网页,但切换到webdriver.Remote连接http://127.0.0.1:4444/wd/hub时失败,报错截图如下:
Remote连接报错

你的Remote模式代码:

from selenium import webdriver

options = webdriver.ChromeOptions()
driver = webdriver.Remote(
    command_executor='http://127.0.0.1:4444/wd/hub',
    options=options
)
driver.get("http://www.google.com")
driver.quit()

本地正常运行的代码:

options = webdriver.ChromeOptions()
# options.add_argument("--headless")
# options.add_argument("--disable-gpu")
driver = webdriver.Chrome(options=options)
driver.get("http://www.google.com")

以下是针对性的排查和解决步骤:

  • 必须先启动Remote WebDriver服务
    本地直接运行webdriver.Chrome时,Selenium会自动启动本地ChromeDriver,但Remote模式需要你手动启动Selenium Hub或Remote服务器:

    • Selenium 4+版本启动命令:
      selenium server start
      
    • 旧版本需下载对应Selenium Server Jar包,执行:
      java -jar selenium-server-4.x.x.jar hub
      

    启动后访问http://127.0.0.1:4444,能看到Selenium Grid控制台说明服务正常。

  • 核对服务器端ChromeDriver与本地Chrome版本
    Remote模式下,服务器端的ChromeDriver版本必须和你本地Chrome浏览器版本完全匹配(大版本+小版本都要一致)。本地运行时Selenium 4.6+会自动管理匹配的ChromeDriver,但Remote模式需要服务器端提前配置好对应版本的驱动。

  • 检查端口与连接地址
    确认4444端口没有被其他程序占用,用以下命令排查:

    # Windows
    netstat -ano | findstr :4444
    # Linux/macOS
    lsof -i :4444
    

    如果端口被占用,要么杀掉占用进程,要么修改Selenium服务的启动端口。

  • 调整Remote初始化参数(适配Selenium版本)
    Selenium 4.x推荐的Remote初始化方式可简化,无需指定command_executor,直接传递options即可:

    from selenium import webdriver
    
    options = webdriver.ChromeOptions()
    driver = webdriver.Remote(options=options)
    driver.get("http://www.google.com")
    driver.quit()
    

    若坚持使用command_executor,需确保路径正确,Selenium 4的Hub默认路径确实是/wd/hub。

内容的提问来源于stack exchange,提问作者enmls

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 06:11:00