📜  RSelenium – 按名称搜索

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

RSelenium – 按名称搜索

在本文中,我们将学习如何使用 Rselenium 中的名称定位器搜索元素。更具体地说,我们将学习如何在 Rselenium 中使用findElement(using = 'name', value = 'the name of the element')方法。

句法:

例子:

考虑以下 HTML 代码:

HTML

   
      
                                    
   


R
# sending firstname and last name using the name locator
rsDriver$findElement(using = 'name', value = 'firstname').sendKeys('Ram')
rsDriver$findElement(using = 'name', value = 'lastname').sendKeys('Singh')


R
# for installing the Rselenium package
install.packages("Rselenium")
  
# for loading the Rselenium package
library(RSelenium)


R
rdriver <- rsDriver(browser = "chrome", # browser name
                   port = 8090L, # port number
                   chromever  = "98.0.4758.102", # browser version
)


R
# Creating client object
rseleniumClientObj <- rsDriver$client


R
# Navigating to the URL
rseleniumClientObj$navigate("https://www.google.com/")


R
# using name for finding the element
searchUsingName <- rseleniumClientObj$findElement(using = 'name', value = 'q')
  
# sending keys 
searchUsingName$sendKeys('GeeksforGeeks',key = "enter")


R
# close the RSelenium server and 
# chrome web browser
rseleniumClientObj$close()


R
# Search using the name in RSelenium 
  
# load the required packages
library(Rselenium)
  
# start the Selenium server
rdriver <- rsDriver(browser = "chrome", # browser name
                    port = 2020L, # port number
                    chromever  = "98.0.4758.102", # chrome browser version
)
  
# creating a client object and opening 
# the browser
rseleniumClientObj <- rdriver$client
  
# Navigate to Google.com
rseleniumClientObj$navigate("https://www.google.com")
  
# search using the name locator, here q(query)
# is the name
searchUsingName <- rseleniumClientObj$findElement(using = 'name', 
                               value = 'q')
  
# sending "GeeksforGeeks" as a search query
searchUsingName$sendKeysToElement(list("GeeksforGeeks", key = "enter"))
  
# For closing the browser
rseleniumClientObj$close()


输出:

现在,如果我们想自动填写此表格。我们可以使用findElement by name定位器。我们可以使用下面的代码来填写表格:

代码:

R

# sending firstname and last name using the name locator
rsDriver$findElement(using = 'name', value = 'firstname').sendKeys('Ram')
rsDriver$findElement(using = 'name', value = 'lastname').sendKeys('Singh')

现在,让我们尝试在 R 中借助 Rselenium 来实现它。

分步实施

第 1 步:打开 Rstudio 并创建一个名为searchByName.R的新脚本

第 2 步:使用以下命令将 Rselenium 包导入并加载到 Rstudio 中:

R

# for installing the Rselenium package
install.packages("Rselenium")
  
# for loading the Rselenium package
library(RSelenium)

第 3 步:使用 Chrome 网络驱动程序创建一个新的 Rselenium 服务器。

R

rdriver <- rsDriver(browser = "chrome", # browser name
                   port = 8090L, # port number
                   chromever  = "98.0.4758.102", # browser version
)

这将创建一个新的 Rselenium 服务器并启动 Chrome 网络驱动程序。

第 4 步:使用以下命令创建 Rselenium 服务器的客户端对象以与 Web 浏览器交互:

R

# Creating client object
rseleniumClientObj <- rsDriver$client

第 5 步:使用以下命令导航到 URL [https://www.google.com/]:

R

# Navigating to the URL
rseleniumClientObj$navigate("https://www.google.com/")

第 6 步:要通过名称 locator [q] 查找元素并发送搜索关键字,我们可以使用以下命令:

R

# using name for finding the element
searchUsingName <- rseleniumClientObj$findElement(using = 'name', value = 'q')
  
# sending keys 
searchUsingName$sendKeys('GeeksforGeeks',key = "enter")

这里,q 是 Google 搜索页面中元素的名称。带有sendKeys方法的searchUsingName会将搜索关键字 [“GeeksforGeeks”] 发送到元素,然后按 Enter 键。

第 7 步:要关闭浏览器和服务器,请运行以下命令:

R

# close the RSelenium server and 
# chrome web browser
rseleniumClientObj$close()

R 中的上述代码将关闭 Chrome 网络浏览器和 Rselenium 服务器。

整个 R 程序代码的完整实现:

R

# Search using the name in RSelenium 
  
# load the required packages
library(Rselenium)
  
# start the Selenium server
rdriver <- rsDriver(browser = "chrome", # browser name
                    port = 2020L, # port number
                    chromever  = "98.0.4758.102", # chrome browser version
)
  
# creating a client object and opening 
# the browser
rseleniumClientObj <- rdriver$client
  
# Navigate to Google.com
rseleniumClientObj$navigate("https://www.google.com")
  
# search using the name locator, here q(query)
# is the name
searchUsingName <- rseleniumClientObj$findElement(using = 'name', 
                               value = 'q')
  
# sending "GeeksforGeeks" as a search query
searchUsingName$sendKeysToElement(list("GeeksforGeeks", key = "enter"))
  
# For closing the browser
rseleniumClientObj$close()

输出: