0

I want to scroll down the website by looping through all webElements and I'm doing it in this way:

driver.get('https://justjoin.it/')
driver.maximize_window()

while True:
    for web_element in driver.find_elements_by_class_name('css-1x9zltl'):
        driver.execute_script("arguments[0].scrollIntoView();", web_element)

Unfortunately after few seconds scroll bar stops and console throws me this exception:

Message: stale element reference: element is not attached to the page document 

Looking for solutions I searched a lot of webpages and now I know why I have this type of error. I tried to solve that by using try/except and it works but of course it doesn't solve the essence of the problem. I want to know Is there a better way to solve my issue?

beginsql
  • 135
  • 7

2 Answers2

0

Perhaps you're calling find_elements_by_class_name() before your justjoin.it page is loaded and "stable". Then, while you're looping through the elements you collected, the page continues loading and invalidates those collected element objects. I would try waiting longer between calling driver.get() and calling find_elements_by_class_name().

Conrad Albrecht
  • 1,976
  • 2
  • 15
  • 19
  • I put time.sleep(10) between while True and for web_element in... and console still throws me same error ;/ – beginsql Jul 10 '20 at 10:38
0

Stale element exception occuring when element in on page but selenium driver instance could not interect with that element.

Following actions can be resolve stale element exception

1.Refresh page by using "navigate(). refresh()" method in selenium

2.using loop try to click or check visible of that element if that element visible or already clicked exit from loop

Justin Lambert
  • 940
  • 1
  • 7
  • 13