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

RSelenium连接standalone-firefox容器失败:Empty reply from server

解决RSelenium在Windows环境下的连接问题

问题背景

我正在使用R语言学习RSelenium,尝试了两种配置方式均失败,具体情况如下:

1. Chrome驱动配置尝试

执行以下代码配置Chrome驱动:

library(RSelenium)
library(tidyverse)

driver <- RSelenium::rsDriver(browser = "chrome",
                              chromever =
                                system2(command = "wmic",
                                        args = 'datafile where name="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" get Version /value',
                                        stdout = TRUE,
                                        stderr = TRUE) %>%
                                stringr::str_extract(pattern = "(?<=Version=)\\d+\\.\\d+\\.\\d+\\.") %>%
                                magrittr::extract(!is.na(.)) %>%
                                stringr::str_replace_all(pattern = "\\.",
                                                         replacement = "\\\\.") %>%
                                paste0("^",  .) %>%
                                stringr::str_subset(string =
                                                      binman::list_versions(appname = "chromedriver") %>%
                                                      dplyr::last()) %>%
                                as.numeric_version() %>%
                                max() %>%
                                as.character())
remote_driver <- driver[["client"]] 
remote_driver$navigate("https://www.latlong.net/convert-address-to-lat-long.html")

出现错误:

[1] "Connecting to remote server"
Could not open chrome browser.
Client error message:
Undefined error in httr call. httr output: Failed to connect to localhost port 4567: Connection refused
Check server log for further details.
Warning message:
In RSelenium::rsDriver(browser = "chrome", chromever = system2(command = "wmic",  :
  Could not determine server status.

2. Docker方案尝试

改用Docker部署Selenium节点,执行代码:

shell('docker pull selenium/standalone-firefox')
shell('docker run -d -p 4445:4444 selenium/standalone-firefox')
remDr <- remoteDriver(remoteServerAddr = "localhost", port = 4445L, browserName = "firefox")
remDr$open()
remDr$navigate("http://www.google.com/ncr")
remDr$getTitle()

出现错误:

> shell('docker pull selenium/standalone-firefox')
Using default tag: latest
latest: Pulling from selenium/standalone-firefox
Digest: sha256:b6d8279268b3183d0d33e667e82fec1824298902f77718764076de763673124f
Status: Image is up to date for selenium/standalone-firefox:latest
docker.io/selenium/standalone-firefox:latest

What's Next?
  View summary of image vulnerabilities and recommendations → docker scout quickview selenium/standalone-firefox
> shell('docker run -d -p 4445:4444 selenium/standalone-firefox')
57c117ceca095eb96113804cb8db3c64c197499af26c387b96d141d7d5a445df
> remDr <- remoteDriver(remoteServerAddr = "localhost", port = 4445L, browserName = "firefox")
> remDr$open()
[1] "Connecting to remote server"
Error in checkError(res) : 
  Undefined error in httr call. httr output: Empty reply from server
> remDr$navigate("http://www.google.com/ncr")
Error in checkError(res) : 
  Undefined error in httr call. httr output: length(url) == 1 is not TRUE
> remDr$getTitle()
Error in checkError(res) : 
  Undefined error in httr call. httr output: length(url) == 1 is not TRUE

当前R会话信息

> sessionInfo()
    R version 4.1.3 (2022-03-10)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows 10 x64 (build 22621)
    
    Matrix products: default
    
    locale:
    [1] LC_COLLATE=English_Canada.1252  LC_CTYPE=English_Canada.1252    LC_MONETARY=English_Canada.1252 LC_NUMERIC=C                    LC_TIME=English_Canada.1252    
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     
    
    other attached packages:
    [1] dplyr_1.0.9

请问该如何解决上述问题?是否有其他可行的替代方案?


解决方案

一、修复Chrome驱动连接问题

  1. 检查端口占用
    Windows下打开命令提示符,执行:

    netstat -ano | findstr :4567
    

    若有结果,记录PID,打开任务管理器结束对应进程后重试。

  2. 手动指定ChromeDriver版本
    自动版本检测可能出错,先查看Chrome浏览器版本(设置→关于Chrome),下载对应大版本的ChromeDriver,然后在rsDriver中直接指定版本号:

    driver <- RSelenium::rsDriver(browser = "chrome", chromever = "114.0.5735.90") # 替换为你的Chrome版本对应驱动号
    
  3. 调整权限与安全软件

    • 以管理员身份启动RStudio,避免权限不足导致驱动无法启动
    • 临时关闭Windows Defender防火墙或第三方杀毒软件,防止连接被拦截

二、修复Docker方案问题

  1. 添加共享内存参数
    Firefox容器需要足够共享内存,重新启动容器时加上--shm-size=2g:

    docker run -d -p 4445:4444 --shm-size=2g selenium/standalone-firefox
    
  2. 等待容器完全启动
    Selenium容器启动需要加载浏览器环境,执行remDr$open()前等待1-2分钟,确保服务就绪。

  3. 检查容器状态与日志
    查看容器是否运行:

    docker ps
    

    若容器未启动,用docker start <容器ID>重启;若启动失败,查看日志排查问题:

    docker logs <容器ID>
    
  4. 改用Chrome镜像
    尝试Chrome镜像可能提升兼容性:

    docker pull selenium/standalone-chrome
    docker run -d -p 4445:4444 --shm-size=2g selenium/standalone-chrome
    

    对应R代码修改浏览器名称:

    remDr <- remoteDriver(remoteServerAddr = "localhost", port = 4445L, browserName = "chrome")
    

三、替代方案

  1. 使用chromote包
    chromote是无需额外驱动的Chrome控制工具,配置更简单:

    install.packages("chromote")
    library(chromote)
    # 启动Chrome会话
    b <- ChromoteSession$new()
    # 访问目标页面
    b$Page$navigate("https://www.latlong.net/convert-address-to-lat-long.html")
    # 获取页面标题
    b$Page$getTitle()
    
  2. 使用rvest静态爬取
    如果目标网站不需要JavaScript渲染,直接用rvest爬取更高效:

    install.packages("rvest")
    library(rvest)
    # 读取页面
    page <- read_html("https://www.latlong.net/convert-address-to-lat-long.html")
    # 提取所需内容(示例:提取页面标题)
    page %>% html_element("title") %>% html_text()
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 00:30:02