0

I would like to preface this question by saying that I have viewed all the other questions on this topic and attempted to implement the solutions but am still having no success. I am trying to click on the plus button in the recommendations screen on Quora:

Quora recommnedations screen

Here is my code:

def recommendations():
  time.sleep(10)
  requests = WebDriverWait(driver, 10).until(unhidden_elements_located((By.TAG_NAME, 'svg')))

  for i in range(12):
      try:
          ActionChains(driver).click(requests[i]).perform()

      except selexcep.StaleElementReferenceException as e:
          print(e)

After a wait to let the page load it gets a list of all the plus elements and then goes through and attempts to click them. I am pretty sure it is still getting the correct elements as this code used to work until it randomly started getting this error message:

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

I am at a complete loss of how to solve this problem at this point.

Grant Walton
  • 51
  • 2
  • 7
  • Is it possible after clicking one of the elements that the dom updates/reloads? If so, this is probably causing the issue. – RKelley Mar 02 '19 at 00:13
  • I'm not sure what you mean. I'm not super well versed in this area but if I already clicked on what I wanted to click on why would it matter if the dom updated after that? – Grant Walton Mar 02 '19 at 03:27
  • The html might be getting re-loaded or parts of it changed when an element is clicked. I'm wondering if this is the case and that's what's causing the stale exception to be thrown. You may have to re-find the elements once one is clicked. You might try a for loop based on the original size of the collection found and re-find something like what Pritam is showing in their answer. – RKelley Mar 04 '19 at 18:18
  • I think I figured out what the problem was. The elements I'm trying to click on are in a pop-up window and when I find those elements, I also pick up some elements with the same tags from the main window behind it. – Grant Walton Mar 08 '19 at 09:01

1 Answers1

0

Seems element is not loaded properly after page load. Define you element like below.

search_input = lambda: driver.find_element_by_name('q')

This is because calling 'search_input()' re-evaluates the lambda statement and does a find on the element again.  This prevents staleness by getting a fresh reference to the WebElement each time.  

Muzzamil
  • 2,823
  • 2
  • 11
  • 23