RSelenium UnknownError - java.lang.IllegalStateException 和 Google Chrome

RSelenium UnknownError - java.lang.IllegalStateException with Google Chrome

我运行以下脚本基于RSelenium Basics CRAN page

library(RSelenium)
startServer(args = c("-port 4455"), log = FALSE, invisible = FALSE)
remDr <- remoteDriver(browserName = "chrome")
remDr$open()

这会产生以下错误:

Exception in thread "main" java.net.BindException: Selenium is already running on port 4444. Or some other service is.
 at org.openqa.selenium.server.SeleniumServer.start(SeleniumServer.java:492)
 at org.openqa.selenium.server.SeleniumServer.boot(SeleniumServer.java:305)
 at org.openqa.selenium.server.SeleniumServer.main(SeleniumServer.java:245)
 at org.openqa.grid.selenium.GridLauncher.main(GridLauncher.java:64)

根据 this conversation on GitHub 的评论,我修改了 startServer() 命令,如下所示:

startServer(args = c("-port 4455"), log = FALSE, invisible = FALSE)

然后我在我的控制台中收到以下错误:

Error:   Summary: UnknownError
 Detail: An unknown server-side error occurred while processing the command.
 class: java.lang.IllegalStateException

而弹出的Java提示中的这个错误:

14:38:55.098 INFO - Launching a standalone Selenium Server
14:38:55:161 INFO - Java: Oracle Corporation 25.40-b25
14:38:55.161 INFO - OS: Windows 7 6.1 amd64
14:38:55.161 INFO - v2.46.0, with Core v2.46.0. Built from revision 87c69e2
14:38:55.209 INFO - Driver class not found: com.opera.core.systems.OperaDriver
14:38:55.209 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered
14:38:55:289 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4455/wd/hub
14:38:55:289 INFO - Selenium Server is up and running

我不确定缺少 Opera 驱动程序是实际错误还是只是警告。无论如何,我想使用 Chrome,所以这似乎无关紧要。我做错了什么?

我终于能够通过将来自许多不同来源的信息拼凑在一起来让 RSelenium 工作。我认为将所有这些信息放在一个位置会很有帮助,所以这是我让 RSelenium 在 Windows 7(64 位)上工作的过程,其中 Chrome 作为浏览器:

  1. 下载 64-bit version of Java 我无法使用标准下载。
  2. 下载ChromeDriver.
  3. 从 R.
  4. 下载 Selenium Standalone Server 或 运行 checkForServer()
  5. 创建一个批处理文件来启动 Selenium 服务器。 我最初尝试在 R 脚本中使用 startServer(),但它会经常出现卡住了,没有继续执行脚本中的下一行。这是我创建的批处理文件:

    java -jar C:\path\to\selenium-server-standalone.jar -Dwebdriver.chrome.driver=C:\path\to\chromedriver.exe
    

    Chrome驱动可以放在PATH环境变量中,但是我决定把Chrome驱动的路径添加到批处理文件中(达到同样的目的)。

  6. 运行 R 脚本。 这是我的最终脚本:

    library(RSelenium)
    shell.exec(paste0("C:\path\to\yourbatchfile.bat"))
    Sys.sleep(5)
    
    remDr <- remoteDriver(browserName = "chrome")
    remDr$open(silent = TRUE)
    remDr$navigate("http://www.google.com")
    

    Sys.sleep() 调用是必要的,因为如果 运行 在 Selenium 服务器完成启动之前,我会在 remoteDriver() 调用中收到错误。

值得注意的是,RSelenium 与 OSX 有一些恼人的差异。当您分别 运行 yourcommand.command 文件和 remDr$open() 方法时,invisible=T/silent=T 参数将不起作用。 invisible=T 实际上会提醒你它只适用于 Windows。没什么大不了的(如果有人有解决方法,我将不胜感激)。

为了后代的缘故,OSX 有一个细微的变化,使用 .command 文件而不是具有与上面相同内容的 .bat 文件来替换 shell.exec:

yourcommand.command 文件内容

java -jar /path/to/selenium-server-standalone.jar -Dwebdriver.chrome.driver=/path/to/chromedriver

R脚本修改

library(RSelenium)
system(paste("open","/path/to/yourcommand.command"))
Sys.sleep(5)
...