R – How to Search by class in RSelenium
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
library (Rselenium)
library (tidyverse)
library (netstat)
rdriver <- rsDriver (browser = "chrome" ,
port = 8080L,
chromever = "98.0.4758.102" ,
)
robj <- rdriver$client
translate_element <- robj$ findElement (using = 'class name' ,
value = 'gfg-icon_translate' )
translate_element$ clickElement ()
lang <- robj$ findElement (using = 'class name' ,
value = 'goog-te-combo' )
lang$ clickElement ()
option <- robj$ findElement (using = 'xpath' ,
value = '//*[@id=":0.targetLanguage"]/select/option[76]' )
option$ clickElement ()
robj$ close ()
|
Output:
Last Updated :
07 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...