Open In App

How to open Google Chrome with RSelenium?

Last Updated : 28 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In the article, we are going to learn how to open a Chrome browser using Rselenium package and how to visit a URL. To do so, we must have the following package installed onto our system:

  • Java
  • R and Rstudio
  • Rselenium
  • Web Driver

Installation

  • Java: We have to install java before using the Rselenium package to avoid any error. We can install Java using the following tutorials:

For Linux: https://www.geeksforgeeks.org/how-to-install-java-jdk9-on-linux/

For Windows: https://www.geeksforgeeks.org/how-to-download-and-install-java-for-64-bit-machine/

For Mac: https://www.geeksforgeeks.org/how-to-install-java-on-macos/

  • R and Rstudio: R binary is required to compile the R code. Rstudio is required to run the R code. We can install R and Rstudio by referring the following articles:

For installing R programming language, go to the official site of R programming and download R for Windows(or Mac).

For installing Rstudio you can refer to this article: https://www.geeksforgeeks.org/how-to-install-r-studio-on-windows-and-linux/

  • Rselenium: Rselenium is required to automate the web. We can install Rselenium using the following command:

Command:

install.packages(“RSelenium”)

Output:

This will install the Rselenium package into the Rstudio.

Step by step instructions to open the Chrome web browser using Rselenium:

Step 1: Open up the Rstudio and create a new script named openingChrome.R

Step 2: Import the Rselenium package into the Rstudio by using the following command:

R




library(RSelenium)


Step 3: Create a new Rselenium server using the Chrome web driver.

R




rdriver <- rsDriver(browser = "chrome", # browser name
                   port = 8090L, # port number
                   chromever  = "98.0.4758.102", # browser version
)


This will create a new Rselenium server and will start the Chrome web driver.

Step 4: Create a client object of the Rselenium server to interact with the web browser by using the following command:

R




rseleniumClientObj <- rsDriver$client


Step 5:  Navigate to the URL [https://www.geeksforgeeks.org/] using the following command:

R




rseleniumClientObj$navigate("https://www.geeksforgeeks.org/")


Step 6: To close the browser and server, run the following command:

R




rseleniumClientObj$close()


The above piece of code in R will close the Chrome web browser and the Rselenium server.

Below is the complete implementation.

R




# Opening the Chrome web browser using the RSelenium 
  
# load the required packages
library(Rselenium)
  
# start the Selenium server
rdriver <- rsDriver(browser = "chrome", # browser name
                    port = 2180L, # port number
                    chromever  = "98.0.4758.102", # chrome browser version
)
  
# creating a client object and opening the browser
rseleniumClientObj <- rdriver$client
  
  
# For navigating to the url
rseleniumClientObj$navigate("https://www.geeksforgeeks.org/")
  
# For closing the browser
rseleniumClientObj$close()


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads