Intro

This is the definitive guide to setting up RSelenium on a Mac machine. (By definitive I mean that these steps worked on two different machines so there’s at least a 97.34% chance it’ll work on yours, too!)


Getting the Tools

In order to use RSelenium, we will obviously need to install and load the package.

#install.packages("RSelenium")
library(RSelenium)

Before we touch anything else in R, we’ll need to do some work on the rest of computer.

The first step is to download the latest version of the Selenium Standalone Server. At time of writing, that’s 2.53.1.

Even if you think you have the current version, complete this step. I thought I had the most recent version once. Once.


Once downloaded, you .jar file will be named “selenium-server-standalone-2.53.1-2.jar”. Change its name to “selenium-server-standalone.jar”.

Move this .jar file to the same directory as the script you’re looking to use Selenium. This should be the working directory of your R Project.


Now, you’ll need to get yourself a fresh version of Java.

Again — I cannot emphasize this enough — complete this step even if you’re certain your Java is current. A minute-long install is a far more pleasant experience than the hours of pain RSelenium will inflict on Mac users with old Java versions.


Lastly, you’ll need to have Firefox installed on your machine. Yeah, I know — I can practically hear you ‘Chrome Master Race’ people groaning as I write this.

And hey, it could be worse — I could have written this tutorial using Selenium with Internet Explorer.


Using the Tools

Now that you have the proper tools in your toolbox, you’re almost ready to start automating your browser and copping Yeezys or whatever it is the kids are up to on the Internet these days.

Open Terminal and navigate to the directory containing the Selenium Standalone Server. If you’re a command line n00b, check this out, and use something like this, but with your own path:

cd ~/Desktop/iX/DataScience/just_selenium_things

Double check that “selenium-server-standalone.jar” actually made it in this folder with this complex command:

ls

Now that we’re here, we’re going to launch the Selenium Standalone Server ourselves.

The standard command to do this is:

java -jar selenium-server-standalone.jar

BUT

Here’s the major key: the default IP port for Selenium is Port 4444. For reasons beyond me, using this port for RSelenium will cause many Mac users to crash and burn.

So, instead of using the default port, we’re going to specify our own. I randomly chose 5556, and it worked. Hopefully that’s the case for everyone else:

java -jar selenium-server-standalone.jar -port 5556

Now leave that terminal window alone, and head back to RStudio.

Since we launched the Selenium Server manually, we can skip calling startServer() like we normally would at this point.

Additionally, since we specified our own port, we’ll need to add that to our call to remoteDriver():

browser <- remoteDriver(port = 5556)

We can now proceed as normal — as long as you don’t close the terminal you launched the Selenium Server in, everything should now work properly. Happy (automated) Browsing!

browser$open()
browser$navigate("http://meowmania.jqln.org")
elem <- browser$findElement(using = 'css selector', 'body > object')
for(c in 1:50){
  elem$clickElement()
  Sys.sleep(2)
}
## [1] "Connecting to remote server"
## $applicationCacheEnabled
## [1] TRUE
## 
## $rotatable
## [1] FALSE
## 
## $handlesAlerts
## [1] TRUE
## 
## $databaseEnabled
## [1] TRUE
## 
## $version
## [1] "47.0.1"
## 
## $platform
## [1] "MAC"
## 
## $nativeEvents
## [1] FALSE
## 
## $acceptSslCerts
## [1] TRUE
## 
## $webdriver.remote.sessionid
## [1] "81905ef2-f793-4297-9c27-b4308033840b"
## 
## $webStorageEnabled
## [1] TRUE
## 
## $locationContextEnabled
## [1] TRUE
## 
## $browserName
## [1] "firefox"
## 
## $takesScreenshot
## [1] TRUE
## 
## $javascriptEnabled
## [1] TRUE
## 
## $cssSelectorsEnabled
## [1] TRUE
## 
## $id
## [1] "81905ef2-f793-4297-9c27-b4308033840b"