将RSelemium与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()
}

最新更新