Open In App

Send mouse events to elements using Rselenium in R

Last Updated : 23 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 RSelenium package into the Rstudio using the following code:

R




# load library
library(RSelenium)


Step 3: Create a new Rselenium server with the Chrome web drivers. This will create a new Rselenium server and will start the Chrome web driver.

R




# start the Selenium server
rdriver <- rsDriver(browser = "chrome", # browser name
                    port = 3230L, # port number
                    chromever  = "98.0.4758.102", # chrome browser version
)


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

R




# creating a client object and
# opening the browser
remDr <- rdriver$client


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

R




# navigate to the url
remDr$navigate("http://www.google.com/ncr")


Step 6: Select the search element by CSS using the following code.

R




# finding query css from the page
webElem <- remDr$findElement(using = "css", "[name = 'q']")


Here, q represents the query. Now, to send the search keyword, we are using the sendKeysToElements method.

R




# sending the search keyword
webElem$sendKeysToElement(list("GeeksforgGeeks", key = "enter"))


Step 7: Now, we will be finding all the URL links from the search result using the following code. After that, we are going to store all the URL links into a list. Then we are going to find the URL title that matches our search keyword.

R




# finding all the URL links from the search result
webElems <- remDr$findElements(using = "css selector", "h3")
 
# storing the list of titles
resHeaders <- unlist(lapply(webElems, function(x) {x$getElementText()}))
 
# checking if the title matches within the list of URLs
webElem <- webElems[[which(resHeaders == "GeeksforGeeks |\
A computer science portal for geeks")]]


Step 8: Now, we will simply send a mouse event click to click on the URL link.

R




# clicking on the URL
webElem$clickElement()


Step 9: Closing the Rselenium browser and server.

R




# closing the server
remDr$close()


Below is the complete implementation:

R




# R program to demonstrate mouse event using Rselenium
 
# load the required packages
library(Rselenium)
 
# start the Selenium server
rdriver <- rsDriver(browser = "chrome", # browser name
                    port = 3230L, # port number
                    chromever  = "98.0.4758.102", # chrome browser version
)
 
# creating a client object and opening the browser
remDr <- rdriver$client
 
# navigate to the url
remDr$navigate("http://www.google.com/ncr")
 
# finding query css from the page
webElem <- remDr$findElement(using = "css", "[name = 'q']")
 
# sending the search keyword
webElem$sendKeysToElement(list("GeeksforgGeeks", key = "enter"))
 
# finding all the URL links from the search result
webElems <- remDr$findElements(using = "css selector", "h3")
 
# storing the list of titles
resHeaders <- unlist(lapply(webElems, function(x) {x$getElementText()}))
 
# checking if the title matches within the list of URLs
webElem <- webElems[[which(resHeaders == "GeeksforGeeks | A \
computer science portal for geeks")]]
 
# clicking on the URL
webElem$clickElement()
 
# closing the server
remDr$close()


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads