Skip to content
Home ยป Starting a remote Selenium server in R

Starting a remote Selenium server in R

  • by
  • 3 min read
Tags:

In this brief article, I explain how you can run a Selenium server, right from within your R code. This allows you to not manually open and run a Selenium command in your command prompt.

This code has been tested on a Win11 machine.

Prerequisites

To run Selenium from within R, there are three prerequisites.

Starting server & client

Everything is in place: you can now run the following code to start a Selenium server, and connect to it.

library('RSelenium')
shell('start cmd.exe /K "java -jar selenium-server-standalone-3.9.1.jar -port 4444"')
rem_driver <- remoteDriver(remoteServerAddr = 'localhost',
                           browserName = 'chrome',
                           port=4444L)

Now that both client and server are running, you can use the following code to navigate to a website, print the active URL and finally close the connection.

rem_driver$open()
rem_driver$navigate("https://www.roelpeters.be")
rem_driver$getCurrentUrl()
rem_driver$close()

Possible error messages

Since there are so many requirements for this to work, it is very prone to error. Here’s an overview.

Error 1

Solve the following error by putting the ChromeDriver in your project directory. Use getwd() and setwd() if necessary.

Selenium message:no such window: target window already closed
from unknown error: web view not found

...
Driver info: driver.version: unknown

Error: 	 Summary: NoSuchWindow
 	 Detail: A request to switch to a different window could not be satisfied because the window could not be found.
 	 class: org.openqa.selenium.NoSuchWindowException
	 Further Details: run errorDetails method

Error 2

Solve the following error by putting Selenium in your project directory. Use getwd() and setwd() if necessary. As an alternative, you can also change the path in the shell command to the location of your Selenium file.

Error: Unable to access jarfile selenium-server-standalone-3.9.1.jar

Error 3

Solve the following error by explicitly setting the browserName argument to ‘chrome’.

Selenium message:Unable to create new service: GeckoDriverService
...
Driver info: driver.version: unknown

Error: 	 Summary: SessionNotCreatedException
 	 Detail: A new session could not be created.
	 Further Details: run errorDetails method

Error 4

Solve the following error by making sure that the command prompt window (opened by the shell() function) is still open. Otherwise, the server is not running, and the client cannot connect to it.

Error in checkError(res) : 
  Undefined error in httr call. httr output: Failed to connect to localhost port 4444 after ... ms: Connection refused

Error 5

In all likelihood, the following error is thrown because you have multiple Selenium server instances running. Close all command prompt windows before starting a new server.

ERROR - Port 4444 is busy, please choose a free port and specify it using -port option

Leave a Reply

Your email address will not be published. Required fields are marked *