Search by ID RSelenium in R
Last Updated :
23 Jul, 2025
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("https://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
obj$navigate("https://www.geeksforgeeks.org/")
# 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:
Similar Reads
RSelenium - Search by name In the article, we are going to learn how we can search an element using the name locator in Rselenium. To be more specific, we are going to learn how to use the findElement(using = 'name', value = 'the name of the element') method in Rselenium. Syntax: rsDriver$findElement(using = 'name', value = '
3 min read
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
3 min read
How to set up RSelenium for R? In this article, we will discuss how to set up RSelenium for the R programming language. Steps for installation of  RSelenium in RStudio and how to use it: Step 1: Install Rstudio onto your system. To install Rstudio on your system, head to the Rstudio website and download the latest version. You ca
2 min read
Waiting for page to load in RSelenium in R In this article, we will discuss how to wait for a page to load in RSelenium. This is a very important functionality that we have to include in our scrapping codes so, that the page gets loaded completely before we scrape the desired part of the code. What is RSelenium? RSelenium is an R Programming
4 min read
Search in COBOL The SEARCH keyword is used to check for the presence or absence of some particular elements in any tables or arrays. We can find an element either by performing loop operations or by using the SEARCH keyword. We can even go for SEARCH ALL but this article will read about the SEARCH keywords only. Sy
2 min read
Send mouse events to elements using Rselenium in R In the article, we are going to learn how to use the Rselenium package to automate web applications. We will also learn how to send mouse events to the web application using RSelenium. Step by Step Implementation Step 1: Create a new file named Rselenium.R in the Rstudio. Step 2: Import the RSeleniu
3 min read