结合使用RSelenium和R中的循环在Web元素中添加迭代值条目

问题描述

我希望我能在这个问题上得到一些帮助:

我正在使用RSelenium从一个站点下载记录,该站点只能批量下载500个记录。我有大量记录要下载,因此如果可以做一个看起来像这样的循环,那就太好了这个伪代码

# step one
navigate to website
# step two
find and click on the webelement 'from'
# step three
enter value 1 in the 'from' element and 500 in the 'to' element (this uses the tab key to move from 'from' to 'to')
# step four
initiate file extraction sequence
# step five
clear the 'from' and 'to' boxes OR renavigate to page
#step six
repeat as above but with value 501 in the 'from' element and 1000 in the 'to' element and extract those files
# step seven
repeat to n

这是我想在其中添加循环的实际代码:

# this navigates to the website of interest.
remdr$navigate("somewebsite")

# here,my code clicks on the web element of interest
webElem4 <- remdr$findElement(using = "css selector","#markFrom") #selects the element for the first number
webElem4$clickElement() # clicks on that element

# this enters the values for the records to be downloaded. In this case 1 - 500
webElem4$sendKeysToElement(list("001",key = "tab","500")) # enters numbers into elements

# this is the sequence of clicks to download the data
webElem2 <- remdr$findElement(using = "css selector","#exportTypeName") # selects the type of file to export
webElem2$clickElement() # clicks on selectien
webElem3 <- remdr$findElement(using = "css selector","#exportButton") # exports selection
webElem3$clickElement()

解决方法

我现在使用以下代码解决了此问题

or (i in 0:99){
  # clear out elements
  webElem2 <- remdr$findElement(using = "css selector","#markFrom") #selects the element for the first number
  webElem2$clearElement()
  webElem3 <- remdr$findElement(using = "css selector","#markTo") #selects the element for the first number
  webElem3$clearElement()
  
  # here,my code clicks on the web element of interest
  webElem4 <- remdr$findElement(using = "css selector","#markFrom") #selects the element for the first number
  webElem4$clickElement() # clicks on that element
  
  # this enters the values for the records to be downloaded. In this case 1 - 500
  webElem4$sendKeysToElement(list(paste(i*500+1),key = "tab",paste(i*500+1+500-1))) # enters numbers into elements
  
  # this is the sequence of clicks to download the data
  webElem5 <- remdr$findElement(using = "css selector","button.quickOutputOther") # selects the type of file to export
  webElem5$clickElement() # clicks on selection
  webElem6 <- remdr$findElement(using = "css selector","#exportButton") # exports selection
  webElem6$clickElement()
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...