Search code examples
rdockerrselenium

can't execute rsDriver (connection refused)


I can't get anywhere with R selenium. Here's the first step and my output:

library(RSelenium)
rD <- rsDriver()
# checking Selenium Server versions:
#   BEGIN: PREDOWNLOAD
# BEGIN: DOWNLOAD
# BEGIN: POSTDOWNLOAD
# checking chromedriver versions:
#   BEGIN: PREDOWNLOAD
# BEGIN: DOWNLOAD
# BEGIN: POSTDOWNLOAD
# checking geckodriver versions:
#   BEGIN: PREDOWNLOAD
# BEGIN: DOWNLOAD
# BEGIN: POSTDOWNLOAD
# checking phantomjs versions:
#   BEGIN: PREDOWNLOAD
# BEGIN: DOWNLOAD
# BEGIN: POSTDOWNLOAD
# [1] "Connecting to remote server"
# Error in checkError(res) : 
#   Undefined error in httr call. httr output: Failed to connect to localhost port 4567: Connection refused
# In addition: Warning message:
#   In rsDriver() : Could not determine server status.

What did I miss ?


Solution

  • Note1: checkForServer() was removed in 1.7.9. I think it doesn't matter and the answer still holds up. If not please comment, I don't use it and don't have a windows machine anymore.

    Note2: this answer is meant for Windows

    Note3: I'm also happy to choose another answer since I was also the one who asked, but my answer has more points so make a case in comments

    When trying to run the defunct checkForServer() Selenium offers two options:

    • use rsDriver
    • use Docker

    see:

    RSelenium::checkForServer()
    # Error: checkForServer is now defunct. Users in future can find the function in 
    # file.path(find.package("RSelenium"), "examples/serverUtils"). The
    # recommended way to run a selenium server is via Docker. Alternatively
    # see the RSelenium::rsDriver function.
    

    Everybody seems to have issues with rsDriver and Docker is the recommended option so we'll go this route:

    • install docker
    • run it, restart computer as requested
    • pull image by running in command line: docker pull selenium/standalone-firefox(or chrome instead of firefox) or in R shell('docker pull selenium/standalone-firefox')
    • start server by running in command line: docker run -d -p 4445:4444 selenium/standalone-firefox or in R shell('docker run -d -p 4445:4444 selenium/standalone-firefox')
    • Then run remDr <- remoteDriver(remoteServerAddr = "localhost", port = 4445L, browserName = "firefox'") . The doc suggests something different with a virtual machine but i couldn't get it to work.

    With this I was set, here is my code:

    shell('docker run -d -p 4445:4444 selenium/standalone-firefox')
    remDr <- remoteDriver(remoteServerAddr = "localhost", port = 4445L, browserName = "firefox")
    remDr$open()
    remDr$navigate("http://www.google.com/ncr")
    remDr$getTitle()
    # [[1]]
    # [1] "Google" 
    

    The doc for more info: