问题
I am running the following script based on the RSelenium Basics CRAN page:
library(RSelenium)
startServer(args = c("-port 4455"), log = FALSE, invisible = FALSE)
remDr <- remoteDriver(browserName = "chrome")
remDr$open()
This produces the following error:
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)
Based on the comments from this conversation on GitHub, I've modified my startServer()
command like so:
startServer(args = c("-port 4455"), log = FALSE, invisible = FALSE)
I then receive the following error in my console:
Error: Summary: UnknownError
Detail: An unknown server-side error occurred while processing the command.
class: java.lang.IllegalStateException
And this error in the Java prompt that pops up:
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
I'm not sure if the lack of an Opera driver is an actual error or just a warning. Regardless, I would like to use Chrome, so it seems like it shouldn't matter. What am I doing wrong?
回答1:
I was finally able to get RSelenium to work by piecing together information from a number of different sources. I think it would be helpful to have all of this information in one location, so here is the process that I went through to get RSelenium to work on Windows 7 (64-bit) with Chrome as the browser:
- Download the 64-bit version of Java. I could not get anything to work with the standard download.
- Download ChromeDriver.
- Download the Selenium Standalone Server or run
checkForServer()
from R. Create a batch file to start the Selenium server. I initially tried to use
startServer()
from an R script, but it would frequently get stuck and not carry on to the next line in the script. Here is the batch file that I created:java -jar C:\path\to\selenium-server-standalone.jar -Dwebdriver.chrome.driver=C:\path\to\chromedriver.exe
ChromeDriver can be put in the PATH environmental variable, but I decided to add in the path to ChromeDriver to the batch file (which accomplishes the same goal).
Run the R script. Here is my final script:
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")
The
Sys.sleep()
call was necessary because I would get an error in theremoteDriver()
call if it ran before the Selenium Server had finished starting.
回答2:
It is worth noting that RSelenium has some annoying differences for OSX. The invisible=T/silent=T arguments will not work when you run the yourcommand.command file and the remDr$open() method, respectively. The invisible=T will actually remind you that it only works on Windows. Not a huge deal (and if someone has a workaround I'd appreciate it).
For posterity's sake here's a slight variation for OSX to replace shell.exec using a .command file instead of a .bat with the same contents as above:
yourcommand.command file contents
java -jar /path/to/selenium-server-standalone.jar -Dwebdriver.chrome.driver=/path/to/chromedriver
R script modification
library(RSelenium)
system(paste("open","/path/to/yourcommand.command"))
Sys.sleep(5)
...
来源:https://stackoverflow.com/questions/31124702/rselenium-unknownerror-java-lang-illegalstateexception-with-google-chrome