📜  R – 如何在 RSelenium 中按类搜索

📅  最后修改于: 2022-05-13 01:55:37.680000             🧑  作者: Mango

R – 如何在 RSelenium 中按类搜索

在本文中,我们将学习如何使用 Rselenium 包来自动化 Web 应用程序。

Rselenium是一个免费的开源工具,用于自动化网络。 Rselenium 类似于流行的与Python一起工作的Selenium ,它是一个成熟的网络自动化工具。它是用Java编写的,可用于自动化任何 Web 应用程序。在 R 编程中,我们有一个 Rselenium 包,可以用来在网页上进行各种单元测试。 Rselenium 是一个很棒的工具,它可以以比任何其他软件包更有效的方式自动化 Web 浏览器和抓取数据。

我们还将学习如何使用 Rselenium 包按类名搜索元素。更具体地说, findElement(using = "class name", value = "the class name")方法用于通过类名搜索元素。

句法:

robj$findElement(using = "class", "class name")

例子:

HTML

   
      RSelenium Demonstration
   
   
      
         Welcome To GFG       
   


R
# R program to demonstrate RSelenium
# search element using the class name
 
# load the required packages
library(Rselenium)
library(tidyverse)
library(netstat)
 
 
# start the Selenium server
rdriver <- rsDriver(browser = "chrome", # browser name
                    port = 8080L, # port number
                    chromever  = "98.0.4758.102", # chrome browser version
                    )
 
# creating a client object and opening the browser
robj <- rdriver$client
 
 
# navigate to the url
robj$navigate("https://www.geeksforgeeks.org/")
 
 
# search for the translate webpage element
# using the class name
translate_element <- robj$findElement(using = 'class name' ,
                        value = 'gfg-icon_translate')
# clicking on the translate icon
translate_element$clickElement()
 
 
# displaying all the supported language
lang <- robj$findElement(using = 'class name',
                         value =  'goog-te-combo')
lang$clickElement()
 
 
# selecting the punjabi language from the dropdown list
option <- robj$findElement(using = 'xpath',
                           value = '//*[@id=":0.targetLanguage"]/select/option[76]')
 
# clicking on the selected option
option$clickElement()
 
 
# closing the browser
robj$close()



现在,如果我们想按类名搜索元素。首先,我们必须导入必要的包。然后初始化驱动程序。然后我们必须使用findElement (using = "class name", value = "the class name") 方法来搜索元素。

如何使用 Rselenium 包按类名搜索元素?

让我们实际实现上述搜索方法。为此,我们将使用 geeksforgeeks 官方网站。我们正在尝试使用网页右上角显示的谷歌翻译图标翻译整个网页。为了达到这个目的,首先我们将使用类名选择图标并单击元素。点击翻译后,会显示各种语言。我们将选择旁遮普语并将网页翻译成旁遮普语,翻译成功后我们将使用 close 方法关闭 Rselenium 服务器和浏览器。

创建一个名为 class_search.R 的 R 程序来演示上述搜索方法。

R

# R program to demonstrate RSelenium
# search element using the class name
 
# load the required packages
library(Rselenium)
library(tidyverse)
library(netstat)
 
 
# start the Selenium server
rdriver <- rsDriver(browser = "chrome", # browser name
                    port = 8080L, # port number
                    chromever  = "98.0.4758.102", # chrome browser version
                    )
 
# creating a client object and opening the browser
robj <- rdriver$client
 
 
# navigate to the url
robj$navigate("https://www.geeksforgeeks.org/")
 
 
# search for the translate webpage element
# using the class name
translate_element <- robj$findElement(using = 'class name' ,
                        value = 'gfg-icon_translate')
# clicking on the translate icon
translate_element$clickElement()
 
 
# displaying all the supported language
lang <- robj$findElement(using = 'class name',
                         value =  'goog-te-combo')
lang$clickElement()
 
 
# selecting the punjabi language from the dropdown list
option <- robj$findElement(using = 'xpath',
                           value = '//*[@id=":0.targetLanguage"]/select/option[76]')
 
# clicking on the selected option
option$clickElement()
 
 
# closing the browser
robj$close()

输出: