You need to refactor your code. Your code pattern is likely something like this (of course with different id-s but since you did not include your code or the page source this is the best I can offer):
container = driver.find_elements_by_xpath('//*[@class="window_of_elements"]')
elements = container.find_elements_by_xpath('//*[@class="my_selected_class"]')
for e in elements:
minus_part = e.find_element_by_xpath('//span[@class="remove"]')
minus_part.click()
When you click the minus_part
, the container of your elements
is probably getting re-rendered/reloaded and all your previously found elements turn stale
.
To bypass this you should try a different approach:
container = driver.find_elements_by_xpath('//*[@class="window_of_elements"]')
to_be_removed_count = len(container.find_elements_by_xpath('//*[@class="my_selected_class"]'))
for _ in range(to_be_removed_count):
target_element = container.find_element_by_xpath('//*[@class="window_of_elements"]//*[@class="my_selected_class"]')
minus_part = target_element.find_element_by_xpath('//span[@class="remove"]')
minus_part.click()
So basically you should:
- find out how many elements you should find to be clicked
- in a for loop find and click them one by one