Open In App

R – How to Search by class in RSelenium

Improve
Improve
Like Article
Like
Save
Share
Report

In the article, we are going to learn how to use Rselenium package to automate web applications.

Rselenium is a free, open-source tool for automating the web. Rselenium is similar to the popular Selenium that works with python, it is a full-fledged web-automation tool. It is written in Java and can be used to automate any web application. In R programming, we have a Rselenium package that can be used to carry out various kinds of unit testing on the webpage. Rselenium is a great tool for automating web browsers and scraping data in a much more efficient manner than any other package.

We will also learn how to use Rselenium package to search an element by class name. More specifically, findElement(using = “class name”, value = “the class name”) method is used to search an element by class name.

Syntax:

robj$findElement(using = "class", "class name")

Example:

HTML




<html>
   <head>
      <title>RSelenium Demonstration</title>
   </head>
   <body>
      <div class="class1">
         <h class="class2">Welcome To GFG</h>
      </div>
   </body>
</html>


 
 

Now, if we want to search for the element by class name. Firstly, we have to import the necessary packages. Then initialize the driver. Then we have to use findElement(using = “class name”, value =  “the class name”) method to search the element.

 

class_content <- rdriver$findElement(using = “class name”, value = “class2”)

How to use Rselenium package to search an element by class name?

 

Let’s implement the above search method practically.  For this, we will be using the geeksforgeeks official website. And we are trying to translate the whole webpage using the Google Translate icon presented on the top right side of the webpage. For achieving this purpose firstly we will select the icon using the class name and click on the element. After clicking on the translation, it will display various languages. We will select the Punjabi language and translate the webpage into Punjabi language and after successfully translating we will close the Rselenium server and browser using the close method.

 

Create an R program called class_search.R to demonstrate the above search method.

 

R




# R program to demonstrate RSelenium
# search element using the class name
 
# load the required packages
library(Rselenium)
library(tidyverse)
library(netstat)
 
 
# start the Selenium server
rdriver <- rsDriver(browser = "chrome", # browser name
                    port = 8080L, # port number
                    chromever  = "98.0.4758.102", # chrome browser version
                    )
 
# creating a client object and opening the browser
robj <- rdriver$client
 
 
# navigate to the url
 
 
# search for the translate webpage element
# using the class name
translate_element <- robj$findElement(using = 'class name' ,
                        value = 'gfg-icon_translate')
# clicking on the translate icon
translate_element$clickElement()
 
 
# displaying all the supported language
lang <- robj$findElement(using = 'class name',
                         value =  'goog-te-combo')
lang$clickElement()
 
 
# selecting the punjabi language from the dropdown list
option <- robj$findElement(using = 'xpath',
                           value = '//*[@id=":0.targetLanguage"]/select/option[76]')
 
# clicking on the selected option
option$clickElement()
 
 
# closing the browser
robj$close()


Output:



Last Updated : 07 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads