0

I am trying to create a web scraper and I ran into problem. I am trying to iterate over elements on the left side of the widget and if name starts with 'a', I want to click on minus sign and move it to the right side. I managed to find all the elements, however, once the element move to the right is side is executed, right after that loop I get the following error.

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

(Session info: chrome=80.0.3987.163)

JS widget.

Robert J Axe
  • 23
  • 1
  • 4
  • I had this problem too. After you click, the state of your browser has changed, and now the reference to your elements is "stale". Try getting a new list of your elements after each time you click inside the loop. Then click on the next element. – Eric M Apr 16 '20 at 19:26
  • Can you post your code and the link of the page – 0m3r Apr 16 '20 at 23:45

1 Answers1

0

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
Trapli
  • 1,517
  • 2
  • 13
  • 19