I am trying to get the program to scroll in a specific div to load all the needed elements contained in it. As far as I can tell, the JavaScript code is correct but it keeps giving me this error:

selenium.common.exceptions.JavascriptException: Message: TypeError: document.getElementsByClassName(...)[0] is undefined

This is the line the error refers to:

bot.execute_script("document.getElementsByClassName('uiScrollableAreaBody')[0].scrollTo(0,1000)")
1

There are 1 best solutions below

0
On BEST ANSWER

Seems to be the script is trying to get the element before it loads on the page. Try using the explicit wait or time.sleep(x) (Personally I prefer the first approach so that you script does not halt for x number of seconds.

Here is the pseudo code.

element = WebDriverWait(driver,30).until(lambda x: x.execute_script("return document.getElementsByClassName('uiScrollableAreaBody')[0]"))
# now you can perform operation on the element