i am trying to learn the RSelenium package so that I can automate pulling data from webpages. When I am doing this I am encountering a strange error.
"no such element: Unable to locate element: {\"method\":\"css selector\",\"selector\":\"[name='rankings_table_length']\"}
(Session info: chrome=87.0.4280.88)
What makes it strange is that I am able to run the script perfectly fine when I am running each line individually, but when I try to run the lines in a chunk or a loop rselenium is unable to find the elements. I know that I have the elements correct from inspecting them. Does anyone with more RSelenium experience know why code might work when running each line individually, but not in a chunk?
Full code below for reproducibility:
conn <- rsDriver(browser = "chrome",
port = 950L,
chromever = "87.0.4280.88")
chooseStat <- c("Total Offense",
"Total Defense",
"3rd Down Conversion Pct"
)
year <- "2020"
conn$client$open()
conn$client$navigate("https://stats.ncaa.org/")
sportInput <- conn$client$findElement(using = "css", "[name = 'sport']")
sportInput$clickElement()
sportInput$sendKeysToElement(list("football", key = "enter"))
yearInput <- conn$client$findElement(using = "css", "[name = 'acadyr']")
yearInput$clickElement()
yearInput$sendKeysToElement(list(year, key = "enter"))
divInput <- conn$client$findElement(using = "css", "[name = 'u_div']")
divInput$clickElement()
divInput$sendKeysToElement(list("FBS", key = "enter"))
selectTeam <- conn$client$findElement(using = "css", "[id = 'stat_type_T_N']")
selectTeam$clickElement()
statInput <- conn$client$findElement(using = "css",
"[id = 'Stats']")
statInput$clickElement()
statInput$sendKeysToElement(list(chooseStat[1], key = "enter"))
lenInput <- conn$client$findElement(using = "css selector",
"[name='rankings_table_length']")
lenInput$clickElement()
lenInput$sendKeysToElement(list("130", key = "enter"))
tbl <- conn$client$getPageSource()[[1]] %>%
readHTMLTable()
stat_tbl <- tbl$rankings_table
assign(str_replace_all(chooseStat[1], " ", "_"), stat_tbl)