Open In App

Search by ID RSelenium in R

Last Updated : 24 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In the article, we discuss how to use Rselenium package to automate web applications. We will also learn how to use RSelenium to search an element by the id. More specifically, findElement(using = “id”, value = “the id value”) method is used to search an element by id.

Syntax:

object$findElement(using= "id", "value")

Example:

HTML




<html>
   <head>
       <title> Rselenium Demo </title>
   </head>
   <body>
       <input type="text" id="txt1" value="Hello">
       <input type="text" id="txt2" value="World">
   </body>
</html>


Output

Now, if we want to search for the element by id, we can use the following code in R:

id_content <- robj$findElement(using = “id”, value = “txt1”)

Here, we are using the robj$findElement() method to search for the element by id. The robj$findElement() method takes two arguments. The first argument is the using argument and the second argument is the value argument. The using argument is used to specify the type of element to be searched. The value argument is used to specify the value of the element to be searched.

Stepwise Implementation

Step 1: Create a new file named Rselenium.R in the Rstudio.

Step 2: Import the RSelenium package into the Rstudio using the following code:

# loading the Rselenium package
library(RSelenium)

Step 3: Create a new Rselenium server with the Chrome web drivers.

driver <- rsDriver(browser = "chrome", # chrome browser
                   port = 4444, # default port
                   chromever = "latest", # latest version of chrome)

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

Step 4: Create a new client object from the Rselenium server that we created earlier.

obj <- rsClient$client

Step 5: Open the URL in the browser using the following code.

obj$navigate("http://www.geeksforgeeks.org/")

Step 6: Find the element by the id using the following code. We are going to select the button scroolTotop in the Rselenium using the element by id.

# selecting scroll top button using the element id

id_content <- obj$findElements(using = “id”,”scrollTopBtn”)[[1]]

Step 7: Clicking on the button using the following code.

# clicking on the scroll to top button
id_content$clickElement()

Step 8: Closing the Rselenium browser and server.

# closing the browser
obj$close()

Below is the complete implementation:

Script:

R




# R program to demonstrate RSelenium to
# search element using the id
 
# load the required packages
library(Rselenium)
 
# start the Selenium server
rdriver <- rsDriver(browser = "chrome",
                    port = 5050L,
                    chromever  = "98.0.4758.102",
)
 
# creating a client object and opening
# the browser
obj <- rdriver$client
 
# navigate to the url
 
# selecting scroll top button using the element id
id_content <- obj$findElements(using = "id","scrollTopBtn")[[1]]
 
# clicking on the scroll to top button
id_content$clickElement()
 
# closing the browser
obj$close()


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads