rselenium with looping

I would be extremely grateful for any help with the following.

Using rselenium, I am trying to iterate through, in increments of 500, a series of records from 1 to n, while exporting each batch. Each time, it has to clear the existing numbers from the box or 'ctrl' + 'a' and overtype what is there. See the picture below.

Using the below code

remdr$navigate("somewebsite.com")
webElem1 <- remdr$findElement(using = "css selector", "#markFrom") #selects the element for the first number
webElem1$clickElement() # clicks on that element
### it is here I would like to loop through the numbers, increasing 500 at a time (it must clear the previous selection each time, or reload a new instance of the page
webElem1$sendKeysToElement(list("1001", key = "tab", "1500")) # enters numbers into elements
# then export each batch of 500
webElem2 <- remdr$findElement(using = "css selector", "#exportTypeName") # selects the type of file to export
webElem2$clickElement() # clicks on selection
webElem3 <- remdr$findElement(using = "css selector", "#exportButton") # exports selection
webElem3$clickElement()

Hi,

I'm not sure I fully understand the issue. Is it just the looping part you're having difficulty with? In that case, you can just wrap the whole piece into a loop function that increments the records by 500 each time.

#Set start, end and steps
start = 1
n = 1200
steps = 500

#Loop through
records = c(seq(start, n, steps), (n+1))
for(x in 1:(length(records)-1)){
  
  # This is example of the line you like to edit
  # .. prev code ...
  # webElem1$sendKeysToElement(list(as.character(records[x]), key = "tab", as.character(records[x+1] -1)))
  # ... next code ...
  
  #Remove this, just for showing what it'd look like
  print(sprintf("webElem1$sendKeysToElement(list(%s, key = \"tab\", %s))", 
        as.character(records[x]), as.character(records[x+1] - 1)))
  
}


#> [1] "webElem1$sendKeysToElement(list(1, key = \"tab\", 500))"
#> [1] "webElem1$sendKeysToElement(list(501, key = \"tab\", 1000))"
#> [1] "webElem1$sendKeysToElement(list(1001, key = \"tab\", 1200))"

Created on 2020-08-12 by the reprex package (v0.3.0)

If this is not what you're looking for, please elaborate

Hope this helps,
PJ

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

Yes, I took your idea and changed it slightly. The issue was me being new to looping.

Here was my solution:

for (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()
}