Send mouse events to elements using Rselenium in R
Last Updated :
23 May, 2022
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:
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
rdriver <- rsDriver (browser = "chrome" ,
port = 3230L,
chromever = "98.0.4758.102" ,
)
|
Step 4: Create a new client object from the Rselenium server that we created earlier.
Step 5: Open the URL in the browser using the following code.
Step 6: Select the search element by CSS using the following code.
R
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
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
webElems <- remDr$ findElements (using = "css selector" , "h3" )
resHeaders <- unlist ( lapply (webElems, function (x) {x$ getElementText ()}))
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.
Step 9: Closing the Rselenium browser and server.
Below is the complete implementation:
R
library (Rselenium)
rdriver <- rsDriver (browser = "chrome" ,
port = 3230L,
chromever = "98.0.4758.102" ,
)
remDr <- rdriver$client
webElem <- remDr$ findElement (using = "css" , "[name = 'q']" )
webElem$ sendKeysToElement ( list ( "GeeksforgGeeks" , key = "enter" ))
webElems <- remDr$ findElements (using = "css selector" , "h3" )
resHeaders <- unlist ( lapply (webElems, function (x) {x$ getElementText ()}))
webElem <- webElems[[ which (resHeaders == "GeeksforGeeks | A \
computer science portal for geeks")]]
webElem$ clickElement ()
remDr$ close ()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...