2015-06-29 14 views
4

我運行的基礎上,RSelenium Basics CRAN page以下腳本:RSelenium不明錯誤 - java.lang.IllegalStateException與谷歌瀏覽

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,所以它看起來應該不重要。我究竟做錯了什麼?

+0

你有chromedriver ACCES從你PATH的某個地方出來。 https://sites.google.com/a/chromium.org/chromedriver/ – jdharrison

回答

8

我終於可以通過將來自多個不同來源的信息拼湊在一起來使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服務器已經開始結束。

1

值得注意的是RSelenium對於OSX有些惱人的區別。當您運行yourcommand.command文件和remDr $ open()方法時,不可見= T/silent = T參數將不起作用。不可見= T實際上會提醒你它只能在Windows上工作。不是一個巨大的交易(如果有人有解決方法,我會很感激)。

爲後人的緣故,這裏有一個微小的變化對OSX使用,而不是與相同內容的上方.BAT一個.command文件替換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) 
...