1

I am using rselenium to automate page navigation in Firefox. My rscript imports a data frame for different conditions and creates a custom function which is then called using the apply command. In the function, each column is referenced through something like this:

#create the function
example <- function(dat) {
    webElem$sendKeysToElement(list(dat[[column1]]))
    #Enters text from the column in the web driver
}
apply(df, 1, example) #Should repeat the function for each row of the data

I have multiple columns that I'm referencing in the apply function so that the data from each one can have a different interaction for the web driver - for example, dat[[column2]], dat[[column3]], and so on. Mostly I'm just sending keys (tabs and arrow keys after findElement(using = 'tag name', 'body') or clicking a button that I find with the findElement function and its id.

I have the script executing and it works fine the first time through, but when it starts over I am getting errors. I would like selenium to open the starting url again, and repeat the navigation using the next line of the data frame. But, what happens is r locks up or reports an error:

Error:   Summary: NoSuchDriver
     Detail: A session is either terminated or not started
     class: org.openqa.selenium.NoSuchSessionException
     Further Details: run errorDetails method

I think that it traces back to the findElements calls in the beginning of the function or the sendKeysToElement call. I have tried closing, quitting, and re-initializing the remote driver but it doesn't seem to make a difference. Are there any tips for troubleshooting? Or a good way to do repeated navigation with selenium in r?

1 Answer 1

2

I was able to solve this by recasting my code from an apply function to a for(...) loop. The function I had created was changed minimally.

  • I started the web driver in the beginning of the loop

    remDr <- remoteDriver(extraCapabilities = list(marionette = TRUE))       
    remDr$open(silent = TRUE)
    remDr$navigate(url)
    
  • Throughout the loop I was careful to pass variables to methods relying on rselenium like findElement and sendKeysToElement. That looked something like this:

    nextBtn(pageBody, remDr)
    

    which was shorthand for:

    nextBtn <- function(element=pageBody, driver=remDr) {
      Sys.sleep(.5)
      driver$findElement(using = 'id',value = "NextButton")$clickElement()
      Sys.sleep(2.5)
    }
    

    I think that because the methods were called in separate functions, they required being pointed to the right driver elements (even though I had tried to set the default)

  • And at the end of the for loop, code closed the web driver down:

    remDr$close()
    remDr$quit()
    

The actual call of the loop was

for (i in 1:nrow(df)) {
  surveys(df[i,])
}

The end result is that the loop functions. It opens a new driver instance each time but there weren't errors. References to a specific column were still made with df[[mold0to2]].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.