I’m dealing with a dynamically loaded page.
After the load, the “next” button appears, which I want to click until the full list of divs is loaded (= until the button is no longer there).
But because of the dynamic loading, I need to query the DOM for the button’s existence in the first place.
As it is now, a while
loop looks for the button (with some random waits if it is not – to account for sometimes slow loading). If it is there, the button is clicked and the button listener is set back to NULL
.
This works until the very end, when the button really no longer is there because everything is loaded. The while loop keeps on looking for the button then.
click_count = 0
click_verifier = NULL
while(is.null(click_verifier)){
click_verifier = tryCatch({
driver$findElement("id", "lib-rnl-lib-rnl-laadMeerBtn")},
error = function(e){NULL})
randsleep = sample(seq(0.5, 1.0, by = 0.001), 1)
Sys.sleep(randsleep)
print(paste0('- - - sleeping for: ', randsleep, ' seconds.'))
if(!is.null(click_verifier)){
load_more_button = driver$findElement("id", "lib-rnl-lib-rnl-laadMeerBtn")
load_more_button$clickElement()
click_count = click_count + 1
print(paste0('Clicked: ', click_count, "x"))
click_verifier = NULL
}
}
Is there a neat way to differentiate between (1) continuing looking for the button due to dynamic loading and (2) not looking for the button because it is not coming (because all is loaded)?
EDIT: If I am understanding you correctly your code works as intended until you hit the very last page and then it will keep finding the next button then the loop won’t exit. You can get around this by checking if the element is enabled.
library(RSelenium)
url = 'https://fantasy.espn.com/football/players/projections'
remdr = rsDriver(remoteServerAddr = "localhost",
port = 4445L,
browser = 'firefox')
driver = remdr$client
driver$navigate(url)
next_button_visible = TRUE
while (next_button_visible) {
next_button = driver$findElement(using = 'xpath',
'/html/body/div[1]/div[1]/div/div/div[5]/div[2]/div[3]/div/div/div/div/nav/button[2]')
next_button_visible = next_button$isElementEnabled()[[1]]
next_button$clickElement()
Sys.sleep(5)
}
Created on 2024-12-12 with reprex v2.1.1
1