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

Examples of solutions:

Wrong wording:

all_a = driver.find_elements_by_class(‘classname’)
for a in all_a:
a.click()

This makes it easy to click on the first a, and the page refreshes. If you click the second one, it will report this The error
can be changed to:
counts_a = len(driver.find_elements_by_class(‘class name’))
for i in range(counts_a):
driver.find_element_by_xpath(‘//a[@class=”class name”][i+1]’ ).click()
may be better, of course, there are other ways of writing, probably meaning that you need to reposition it after refreshing, and then use the repositioned element to operate

 

Similar Posts: