docker 无法执行rsDriver(连接被拒绝)

lhcgjxsq  于 2022-12-11  发布在  Docker
关注(0)|答案(4)|浏览(171)

我不能用R selenium 做任何事情。下面是第一步和我的输出:

library(RSelenium)
rD <- rsDriver()
# 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"
# Error in checkError(res) : 
#   Undefined error in httr call. httr output: Failed to connect to localhost port 4567: Connection refused
# In addition: Warning message:
#   In rsDriver() : Could not determine server status.

我错过了什么?

ny6fqffe

ny6fqffe1#

  • 注意:此答案适用于Windows*

当试图运行过时的checkForServer()时,Selenium提供了两个选项:

  • 使用rsDriver
  • 使用Docker

请参阅:

RSelenium::checkForServer()
# Error: checkForServer is now defunct. Users in future can find the function in 
# file.path(find.package("RSelenium"), "examples/serverUtils"). The
# recommended way to run a selenium server is via Docker. Alternatively
# see the RSelenium::rsDriver function.

Everybodyseemsto在rsDriver方面存在问题,建议使用Docker,因此我们将采用以下方法:

  • install docker
  • 运行它,按要求重新启动计算机
  • 通过在命令行中运行以下命令来提取映像:docker pull selenium/standalone-firefox(或chrome而不是firefox)或R中的shell('docker pull selenium/standalone-firefox')
  • 通过在命令行中运行以下命令启动服务器:docker run -d -p 4445:4444 selenium/standalone-firefox或R中的shell('docker run -d -p 4445:4444 selenium/standalone-firefox')
  • 然后运行remDr <- remoteDriver(remoteServerAddr = "localhost", port = 4445L, browserName = "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()
# [[1]]
# [1] "Google"

更多信息请参阅文档:

gv8xihay

gv8xihay2#

如果这仍然有用的话,我今天遇到了同样的问题,并且能够通过安装Java开发工具包(Java SE开发工具包11.0.1)来修复它。
我从我的电脑上收到了一个错误信息,以及这个问题中提到的相同的R错误,它修复了它。
对于一个可重复的例子,我能够复制这个tutorial
sessionInfo()R版本3.4.4(2018年3月15日)平台:x86_64-apple-darwin 15.6.0(64位)运行于以下环境:macOS高塞拉利昂10.13.5

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] fr_CA.UTF-8/fr_CA.UTF-8/fr_CA.UTF-8/C/fr_CA.UTF-8/fr_CA.UTF-8

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

other attached packages:
[1] RSelenium_1.7.5

loaded via a namespace (and not attached):
[1] Rcpp_0.12.17     XML_3.98-1.11    binman_0.1.1     assertthat_0.2.0 rappdirs_0.3.1   bitops_1.0-6    
[7] R6_2.2.2         jsonlite_1.5     semver_0.2.0     httr_1.3.1       curl_3.2         xml2_1.2.0      
[13] subprocess_0.8.3 tools_3.4.4      wdman_0.2.4      yaml_2.1.18      compiler_3.4.4   caTools_1.17.1  
[19] openssl_1.0.1
wn9m85ua

wn9m85ua3#

在使用RSelenium/rsDriver一段时间没有问题之后,我最近收到了一个与OP非常相似的错误信息,但是无法使用Docker来解决它(主要是出于组织原因)。
看一下function documentationrsDriver()默认配置为启动装有最新版本Chrome * 和 * Firefox的Selenium服务器(Gecko)驱动程序,* 即使您已指定运行其他浏览器。* 在我的情况下,我指定browser = "firefox",但新的Chrome驱动程序已在最近一次运行时自动下载,无论出于什么原因,这才是问题的根源。
这不是特别直观,而且"Connection refused"消息也没有真正的帮助,但遵循错误建议并检查服务器日志使我意识到Selenium仍在尝试加载Chrome驱动程序,即使我使用的是Firefox:

rd <- rsDriver(browser = "firefox")
# Yields an error message after doing pre/post downloads:

#> Could not open firefox 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 rsDriver(browser = "firefox" :
#>  Could not determine server status.

rd$server$log()

> $stderr
> [1] "Error: Could not find or load main class c(-Dwebdriver.chrome.driver=\"C:\filepath\to\chromedrivers\109.0.5414.25.chromedriver.exe\","

> $stdout
> character(0)

无论如何,如果你指定rsDriver()运行一个旧的驱动程序版本,或者只是传入NULL,这样它就不会寻找驱动程序,它可能会解决这个问题(它对我很有效)。

# Default parameters of the rsDriver() function:

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,
         ...)

# Specify an older driver version you know works (or just use NULL), 
# even if it doesn't correspond to the browser you're using:

rd <- rsDriver(broswer = "firefox",
               chromever = "109.0.5414.25") # alt: chromever = NULL

如果您正在查找这些驱动程序在由RSelenium自动下载时保存到的文件路径(例如,您可能想查找旧驱动程序版本的名称),在Windows上,它应该是~/AppData/Local/binman
大声喊出this question上的响应,这也帮助我指出了正确的方向!

w9apscun

w9apscun4#

根据Cran的小插曲,我不得不运行:
shell (“对接机IP”)
然后将remoteServerAddr设置为返回的IP。

相关问题