【问题标题】:RSelenium UnknownError - java.lang.IllegalStateException with Google ChromeRSelenium UnknownError - Google Chrome 的 java.lang.IllegalStateException
【发布时间】:2015-09-16 10:46:28
【问题描述】:

我正在基于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 的 cmets,我修改了我的 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,所以这似乎无关紧要。我做错了什么?

【问题讨论】:

标签: java r selenium rselenium


【解决方案1】:

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

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

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

    ChromeDriver 可以放在 PATH 环境变量中,但我决定将 ChromeDriver 的路径添加到批处理文件中(实现相同的目标)。

  5. 运行 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() 调用是必要的,因为如果 remoteDriver() 调用在 Selenium 服务器完成启动之前运行,我会收到错误。

【讨论】:

    【解决方案2】:

    值得注意的是,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)
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 2017-03-25
      相关资源
      最近更新 更多