我正在基于RSelenium Basics CRAN页面运行以下脚本:
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)
基于GitHub上此对话的评论,我对startServer()命令进行了如下修改:
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作为浏览器进行工作的过程:
checkForServer()
java -jar C:\path\to\selenium-server-standalone.jar -Dwebdriver.chrome.driver=C:\path\to\chromedriver.exe
可以将ChromeDriver放入PATH环境变量中,但是我决定将ChromeDriver的路径添加到批处理文件中(该文件可以实现相同的目标)。
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 Server完成启动之前运行该调用,我会在调用中遇到错误。
Sys.sleep()
remoteDriver()