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

AWS上运行RSelenium报错:无法确定服务器状态

AWS Ubuntu实例上RSelenium启动浏览器失败排查

在AWS Ubuntu 22.04实例上运行RSelenium时,启动Firefox或Chrome均出现端口连接被拒绝、无法打开浏览器的错误,本地笔记本同版本环境可正常运行,尝试更换端口、参考Stack Overflow方案后仍无效。相关操作日志如下:

执行rsDriver启动Firefox的报错日志

> library(tidyverse)
> library(RSelenium)
> 
> rD <- RSelenium::rsDriver(
+   port = 4454L,
+   browser = c("firefox"),
+   version = "latest"
+ )
checking Selenium Server versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
checking chromedriver versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
checking geckodriver versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
checking phantomjs versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
[1] "Connecting to remote server"
Could not open firefox browser.
Client error message:
Undefined error in httr call. httr output: Failed to connect to localhost port 4454 after 0 ms: Connection refused
Check server log for further details.
Warning message:
In RSelenium::rsDriver(port = 4454L, browser = c("firefox"), version = "latest") :
  Could not determine server status.

终端Java相关查询结果

ubuntu@ip-172-31-36-112:~$ whereis java
java: /usr/bin/java /usr/share/java /usr/share/man/man1/java.1.gz

ubuntu@ip-172-31-36-112:~$ java -version
openjdk version "11.0.17" 2022-10-18
OpenJDK Runtime Environment (build 11.0.17+8-post-Ubuntu-1ubuntu222.04)
OpenJDK 64-Bit Server VM (build 11.0.17+8-post-Ubuntu-1ubuntu222.04, mixed mode, sharing)

R中执行wdman::selenium()的结果

> sel = wdman::selenium()
checking Selenium Server versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
checking chromedriver versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
checking geckodriver versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
checking phantomjs versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
> sel$process
PROCESS 'file269f704d94ce.sh', finished.
> sel$output()
character(0)
> sel$error()
character(0)
> sel$stop()
[1] FALSE
> sel$log()
$stderr
[1] ""

$stdout
[1] ""

更换端口和浏览器的尝试结果

> rsDriver(port = 4567L,
+          browser = c("chrome", "firefox", "phantomjs", "internet explorer"),
+          version = "latest",
+          chromever = "latest",
+          geckover = "latest",
+          iedrver = NULL,
+          phantomver = "2.1.1",
+          verbose = TRUE,
+          check = TRUE)
checking Selenium Server versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
checking chromedriver versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
checking geckodriver versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
checking phantomjs versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
[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 after 0 ms: Connection refused
Check server log for further details.
$client
[1] "No sessionInfo. Client browser is mostly likely not opened."

$server
PROCESS 'file269f1793fdb5.sh', finished.
Warning message:
In rsDriver(port = 4567L, browser = c("chrome", "firefox", "phantomjs",  :
  Could not determine server status.

R会话信息

> sessionInfo()
R version 4.2.0 alpha (2022-03-28 r82012)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.1 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

locale:
 [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8        LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
 [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C           LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base      

other attached packages:
 [1] RSelenium_1.7.9 forcats_0.5.2   stringr_1.5.0   dplyr_1.0.10    purrr_1.0.0     readr_2.1.3     tidyr_1.2.1     tibble_3.1.8   
 [9] ggplot2_3.4.0   tidyverse_1.3.2

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9          lubridate_1.9.0     binman_0.1.3        ps_1.7.2            assertthat_0.2.1    utf8_1.2.2          R6_2.5.1           
 [8] cellranger_1.1.0    backports_1.4.1     reprex_2.0.2        httr_1.4.4          pillar_1.8.1        rlang_1.0.6         curl_4.3.3         
[15] googlesheets4_1.0.1 readxl_1.4.1        rstudioapi_0.14     googledrive_2.0.0   wdman_0.2.6         munsell_0.5.0       broom_1.0.2        
[22] compiler_4.2.0      modelr_0.1.10       pkgconfig_2.0.3     tidyselect_1.2.0    fansi_1.0.3         crayon_1.5.2        withr_2.5.0        
[29] tzdb_0.3.0          dbplyr_2.2.1        rappdirs_0.3.3      bitops_1.0-7        grid_4.2.0          jsonlite_1.8.4      gtable_0.3.1       
[36] lifecycle_1.0.3     DBI_1.1.3           magrittr_2.0.3      semver_0.2.0        scales_1.2.1        cli_3.5.0           stringi_1.7.8      
[43] fs_1.5.2            xml2_1.3.3          ellipsis_0.3.2      generics_0.1.3      vctrs_0.5.1         tools_4.2.0         glue_1.6.2         
[50] hms_1.1.2           yaml_2.3.6          processx_3.8.0      timechange_0.1.1    colorspace_2.0-3    gargle_1.2.1        caTools_1.18.2     
[57] rvest_1.0.3         haven_2.5.1    

解决方案

1. 安装桌面浏览器

AWS Ubuntu实例默认未预装Firefox/Chrome,需手动安装:

  • 安装Firefox:
    sudo apt update && sudo apt install firefox -y
    
  • 安装Chrome:
    wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
    sudo dpkg -i google-chrome-stable_current_amd64.deb
    sudo apt install -f -y
    

2. 启用无头模式运行

AWS实例无图形界面,必须使用无头浏览器模式,启动时添加对应参数:

  • Firefox无头启动代码:
    rD <- RSelenium::rsDriver(
      port = 4454L,
      browser = "firefox",
      extraCapabilities = list(
        "moz:firefoxOptions" = list(
          args = list("--headless", "--no-sandbox", "--disable-dev-shm-usage")
        )
      )
    )
    
  • Chrome无头启动代码:
    rD <- RSelenium::rsDriver(
      port = 4567L,
      browser = "chrome",
      extraCapabilities = list(
        chromeOptions = list(
          args = list("--headless", "--no-sandbox", "--disable-dev-shm-usage", "--remote-debugging-port=9222")
        )
      )
    )
    

3. 手动启动Selenium Server排查错误

wdman返回空日志无法定位问题,可手动启动Selenium Server查看具体报错:

  1. 获取wdman生成的启动命令:
    Rscript -e "cat(wdman::selenium(retcommand=TRUE))"
    
  2. 复制输出的命令到终端执行,例如:
    java -jar /home/ubuntu/.local/share/binman_seleniumserver/generic/4.8.0/selenium-server-4.8.0.jar -port 4454
    

根据终端输出的错误信息,进一步排查依赖缺失或配置问题。

4. 检查端口与防火墙

  • 检查端口是否被占用:
    sudo lsof -i :4454
    
  • 若实例启用了UFW防火墙,允许对应端口:
    sudo ufw allow 4454/tcp
    

5. 指定兼容的Selenium版本

尝试使用稳定性更高的Selenium 3.x版本,避免新版本与环境兼容问题:

rD <- RSelenium::rsDriver(
  port = 4454L,
  browser = "firefox",
  version = "3.141.59",
  geckover = "0.32.0"
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 09:20:42