Open In App

How to open Google Chrome with RSelenium?

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:

Installation

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



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

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



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/amp/

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:




library(RSelenium)

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




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:




rseleniumClientObj <- rsDriver$client

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




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

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




rseleniumClientObj$close()

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

Below is the complete implementation.




# 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:


Article Tags :