3

I'm trying to iterate over a list of web elements and click on every element in the list, but after the first click I'm getting StaleElementReferenceException. As I understand the page refreshes and the id of the elements get "stale". Is there a workaround? Thanks

elements = driver.find_elements_by_css_selector("span.name")
for element in range(len(elements)):
    elements[element].click()
1
  • It depends on the page and the elements you are trying to click. If you click an element that refreshes the page or part of the page, then any element that was refreshed is going to be stale. If the page never changes, you could loop through an index into the collection of elements and maintain that index through page refreshes to avoid the staleness issue. Commented Mar 27, 2017 at 3:03

1 Answer 1

4

if only the id of element is changed after you click the element, you can use below code:

elements = driver.find_elements_by_css_selector("span.name")
for element in range(len(elements)):
    elements[element].click()
    elements = driver.find_elements_by_css_selector("span.name")
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.