在 R 中使用 Rselenium 将鼠标事件发送到元素
在本文中,我们将学习如何使用 Rselenium 包来自动化 Web 应用程序。我们还将学习如何使用 RSelenium 将鼠标事件发送到 Web 应用程序。
分步实施
第 1 步:在 Rstudio 中创建一个名为 Rselenium.R 的新文件。
第 2 步:使用以下代码将 RSelenium 包导入 Rstudio:
R
# load library
library(RSelenium)
R
# start the Selenium server
rdriver <- rsDriver(browser = "chrome", # browser name
port = 3230L, # port number
chromever = "98.0.4758.102", # chrome browser version
)
R
# creating a client object and
# opening the browser
remDr <- rdriver$client
R
# navigate to the url
remDr$navigate("http://www.google.com/ncr")
R
# finding query css from the page
webElem <- remDr$findElement(using = "css", "[name = 'q']")
R
# sending the search keyword
webElem$sendKeysToElement(list("GeeksforgGeeks", key = "enter"))
R
# finding all the URL links from the search result
webElems <- remDr$findElements(using = "css selector", "h3")
# storing the list of titiles
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")]]
R
# clicking on the URL
webElem$clickElement()
R
# closing the server
remDr$close()
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 titiles
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()
第 3 步:使用 Chrome 网络驱动程序创建一个新的 Rselenium 服务器。这将创建一个新的 Rselenium 服务器并启动 Chrome 网络驱动程序。
R
# start the Selenium server
rdriver <- rsDriver(browser = "chrome", # browser name
port = 3230L, # port number
chromever = "98.0.4758.102", # chrome browser version
)
第 4 步:从我们之前创建的 Rselenium 服务器创建一个新的客户端对象。
R
# creating a client object and
# opening the browser
remDr <- rdriver$client
第 5 步:使用以下代码在浏览器中打开 URL。
R
# navigate to the url
remDr$navigate("http://www.google.com/ncr")
第 6 步:使用以下代码通过 CSS 选择搜索元素。
R
# finding query css from the page
webElem <- remDr$findElement(using = "css", "[name = 'q']")
这里,q 代表查询。现在,要发送搜索关键字,我们使用 sendKeysToElements 方法。
R
# sending the search keyword
webElem$sendKeysToElement(list("GeeksforgGeeks", key = "enter"))
第 7 步:现在,我们将使用以下代码从搜索结果中查找所有 URL 链接。之后,我们将把所有的 URL 链接存储到一个列表中。然后我们将找到与我们的搜索关键字匹配的 URL 标题。
R
# finding all the URL links from the search result
webElems <- remDr$findElements(using = "css selector", "h3")
# storing the list of titiles
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")]]
第 8 步:现在,我们将简单地发送鼠标事件单击以单击 URL 链接。
R
# clicking on the URL
webElem$clickElement()
第 9 步:关闭 Rselenium 浏览器和服务器。
R
# closing the server
remDr$close()
下面是完整的实现:
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 titiles
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()
输出: