0

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)

1 Answer 1

1

This piece of code is where the error is arising:

lenInput <- conn$client$findElement(using = "css selector",
                                "[name='rankings_table_length']")

It is because this CSS Selector represents the path to an element that isn't loaded on the webpage (and therefore can't be operated on) prior to other selenium calls operating on that page. The real problem here is the time it takes for the webpage to reflect these operations (and load that particular element).

I would recommend inserting sys.sleep(2) function calls throughout your code in order to allow the elements to load prior to trying to operating on them. This should enable you to run your code as a chunk as opposed to line by line. Let me know how you go.

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

1 Comment

That worked. I had played around with adding Sys.sleep() but didn’t put it throughout the code. Doing that seems to work. Thank you!

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.