'RSelenium fails to findElement when not called individually

(Apologies for the bad title, improvements are welcome)

When I run the three last lines of the code below individually everything works as expected. It finds the "Primary Data" tab and clicks on it. But my purpose is to put this inside a function, where it does not work. The problem can be reproduced by simply running the three last lines together.

require(rvest)
require(rJava)
require(RSelenium)
driver <- rsDriver(browser = "firefox", verbose = FALSE)
remDr<-driver[["client"]]

remDr$navigate("https://www.bexis.uni-jena.de/ddm/data/Showdata/31215")
webElem <- remDr$findElement(using='id', value= "primarydata")
webElem$clickElement()


Solution 1:[1]

Ok, it was as simple as interposing a Sys.sleep. This works:

remDr$navigate("https://www.bexis.uni-jena.de/ddm/data/Showdata/31215")
Sys.sleep(1)
webElem <- remDr$findElement(using='id', value= "primarydata")
webElem$clickElement()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Angelos Amyntas