【问题标题】:RSelenium Error: Can't Connect to Host; Selenium Server is not runningRSelenium 错误:无法连接到主机; Selenium 服务器未运行
【发布时间】:2023-04-09 19:59:01
【问题描述】:

我收到以下错误:“checkError(res) 中的错误: 无法连接到 http://localhost:4444/wd/hub 上的主机。 请确保 Selenium 服务器正在运行。”

我使用的是 10.9.5 版的 mac,并下载了所有最新版本的软件包和 java。我的代码是:

library(rvest)
library(RSelenium)
library(wdman)
setwd(Path to selenium standalone file)
pJS <- phantomjs(pjs_cmd = "/phantomjs-2.1.1-macosx/bin/phantomjs")
remDr <- remoteDriver(browserName = "phantomjs")
Sys.sleep(5)
remDr$open(silent = FALSE)

然后我得到提到的错误。我尝试在终端中使用“java -jar selenium-server-standalone.jar”命令(在我们使用 cd 命令导航到正确的目录之后)。我尝试在 remoteDriver() 函数中更改我的端口(更改为 4444、5556)。我尝试了各种 Sys.sleep() 时间(最多 20 秒)。当我用谷歌搜索这个错误时,大多数修复都是针对 FireFox 或 Windows 的,不适用于使用 PhantomJS

我还能尝试什么?

【问题讨论】:

    标签: r selenium phantomjs


    【解决方案1】:

    RSelenium::phantom 函数已弃用。这有一个pjs_cmd 参数,我认为您在上面提到过。您可以使用RSelenium 中的rsDriver 函数或wdman 包中的phantomjs 函数:

    library(RSelenium)
    rD <- rsDriver(browser = "phantomjs")
    remDr <- rD[["client"]]
    # no need for remDr$open a phantom browser is already initialised
    remDr$navigate("http://www.google.com/ncr")
    ....
    ....
    # clean up
    rm(rD)
    gc()
    

    或者使用wdman

    library(RSelenium)
    library(wdman)
    pDrv <- phantomjs(port = 4567L)
    remDr <- remoteDriver(browserName = "phantomjs", port = 4567L)
    remDr$open()
    remDr$navigate("http://www.google.com/ncr")
    ...
    ...
    # clean up
    remDr$close()
    pDrv$stop()
    

    【讨论】:

    • 第二个选项对我有用(尽管第一个选项仍然给我同样的错误)。感谢您的帮助!
    • @jdharrison:当我尝试你的第一个选项时,我收到了这个错误:"Could not open phantomjs browser. Client error message: Summary: SessionNotCreatedException Detail: A new session could not be created. Further Details: run errorDetails method Check server log for further details."。知道如何解决吗?谢谢!
    猜你喜欢
    • 1970-01-01
    • 2023-01-03
    • 2017-04-19
    • 2022-10-25
    • 2017-08-19
    • 1970-01-01
    • 2019-10-08
    • 2019-03-21
    • 2011-09-04
    相关资源
    最近更新 更多