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

Moo*_*per 9 r docker rselenium

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.
Run Code Online (Sandbox Code Playgroud)

我错过了什么 ?

小智 27

在使用 RSelenium/rsDriver 一段时间没有出现问题后,我最近收到了与 OP 非常相似的错误消息,但无法使用 Docker 来解决它(主要是出于组织原因)。我是这样解决的:

查看函数文档,发现rsDriver()默认配置为使用最新版本的 ChromeFirefox (Gecko) 驱动程序启动 Selenium 服务器,即使您已指定要运行的其他浏览器也是如此。就我而言,我指定了browser = "firefox",但新的 Chrome 驱动程序在最近运行时自动下载,并且无论出于何种原因,与 Selenium 配合得不好。这就是问题的根本原因。

它不是特别直观,并且该"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)
Run Code Online (Sandbox Code Playgroud)

无论如何,如果您指定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(browser = "firefox",
               chromever = "109.0.5414.25") # alt: chromever = NULL
Run Code Online (Sandbox Code Playgroud)

如果您正在寻找 RSelenium 自动下载时保存这些驱动程序的文件路径(例如,您可能想查找旧驱动程序版本的名称),在 Windows 上,它应该是~/AppData/Local/binman.

对这个问题的回答表示感谢,这也帮助我指明了正确的方向!

  • 很棒的答案!我担心我必须提出安装 docker 的请求(我的公司在许可方面相当严格)。您的解决方案是救星!不敢相信当我使用 Firefox XD 时它正在检查 chrome。 (3认同)
  • 非常感谢您的回答!我对这个问题感到疯狂,找不到解决方案。 (3认同)
  • 感谢您解决这个问题。我所要做的就是添加 `chromever = NULL` (2认同)

Moo*_*per 19

尝试运行时,已弃用的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.
Run Code Online (Sandbox Code Playgroud)

大家 似乎 发生问题rsDriver和码头工人是推荐的选项,所以我们走的这条路:

  • 安装docker
  • 运行它,按要求重新启动计算机
  • 通过在命令行中运行来拉取图像:( docker pull selenium/standalone-firefoxchrome代替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" 
Run Code Online (Sandbox Code Playgroud)

更多信息的文档:

  • 当我运行此命令时,我收到以下错误“httr 调用中未定义错误”。httr 输出:无法连接到本地主机端口 4445:连接被拒绝`` (3认同)