How to solve ‘StaleElementReferenceException: Message: stale element reference: stale element not found’?

Here’s my code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>elements = driver.find_elements(By.CLASS_NAME, 'name')
for i in elements:
i.click()
driver.back()
</code>
<code>elements = driver.find_elements(By.CLASS_NAME, 'name') for i in elements: i.click() driver.back() </code>
elements = driver.find_elements(By.CLASS_NAME, 'name')
for i in elements:
    i.click()
    driver.back()

Error:
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: stale element not found.

When I tried this…

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>elements = driver.find_elements(By.CLASS_NAME, 'name')
for i in range(len(elements)):
elements[i].click()
time.sleep(2)
driver.back()
elements = driver.find_elements(By.CLASS_NAME, 'name')
</code>
<code>elements = driver.find_elements(By.CLASS_NAME, 'name') for i in range(len(elements)): elements[i].click() time.sleep(2) driver.back() elements = driver.find_elements(By.CLASS_NAME, 'name') </code>
elements = driver.find_elements(By.CLASS_NAME, 'name')
for i in range(len(elements)):
    elements[i].click()
    time.sleep(2)
    driver.back()
    elements = driver.find_elements(By.CLASS_NAME, 'name')

…it shows list index out of range error.

First I stored a list of web elements (links) in a list, then I looped through the list and told selenium to click the web element (going to another page and coming back). Whenever it tries to click the second list item, it shows this error: stale element not found.

How can I make selenium click the web element, come back to the original page, and then click the next element without errors?

1

You get StaleElementException when you’re trying to interact with an object after the page has changed.

When you go back and forwards between pages, you’ll also need to ensure you have the correct wait strategy. Selenium’s default is “the page is loaded”, but this does not mean the content is ready, especially if it’s loaded though javascript.

Try using an iterative identifier along with your collection.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#wait strategy example:
driver.implicitly_wait(10)
#collect the items you want to use
elements = driver.find_elements(By.XPATH, '(//*[@class="name"])')
#loop your items as just a count
for i in range(len(elements)):
# find your one specific element based on i. XPATH iterators start at 1, python starts at 0 (so add 1)
# This line inherits the implicit wait time
driver.find_element(By.XPATH, f'(//*[@class="name"])[{i+1}]').click()
#...Do your reason for clicking here...
driver.back()
</code>
<code>#wait strategy example: driver.implicitly_wait(10) #collect the items you want to use elements = driver.find_elements(By.XPATH, '(//*[@class="name"])') #loop your items as just a count for i in range(len(elements)): # find your one specific element based on i. XPATH iterators start at 1, python starts at 0 (so add 1) # This line inherits the implicit wait time driver.find_element(By.XPATH, f'(//*[@class="name"])[{i+1}]').click() #...Do your reason for clicking here... driver.back() </code>
#wait strategy example:
driver.implicitly_wait(10)

#collect the items you want to use
elements = driver.find_elements(By.XPATH, '(//*[@class="name"])')

#loop your items as just a count
for i in range(len(elements)):
   # find your one specific element based on i.  XPATH iterators start at 1, python starts at 0 (so add 1)
   # This line inherits the implicit wait time
   driver.find_element(By.XPATH, f'(//*[@class="name"])[{i+1}]').click()
   
   #...Do your reason for clicking here...
   
   driver.back()

Some notes:

  • I changed your class_name to an xpath equivielent to allow it to be iterated in the identifier
  • try defining driver.implicitly_wait(10) (that’s 10 seconds) once at the start of your script. When doing the find_element(...), this will wait up to 10 seconds for an element to appear before throwing errors – more on wait strategies here
  • If you’re struggling to get this to work, debug the code and walk through each line slowly. A lot of selenium issues arise from synchronisation. You’re the only one who knows the content of your page – so perhaps an explicit wait might work better for you,

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật