Skip to content Skip to sidebar Skip to footer

Python Selenium WebDriverWait And Click Inconsistently Giving StaleElementReferenceException()

Ok, here goes my attempt to explain this problem that I haven't even figured out for myself yet. I'm using Selenium with the python-bindings and seem to have an issue with a page

Solution 1:

I have run into a similar issue before, and I am 99% sure that your problem is the same.

If you check your loop is:

  1. Look for this element
  2. Click
  3. Look for same element
  4. CLick

Normaly after a Click, some page reload or changes occur. This may affect to the element that you are searching for. And if you don't take care, you may end up looking for the element before is reloaded, and when you click it, the element ID already changed, therefore giving you a Stale exception.

Lets go one by one:

  1. Page loads and you element has ID=1
  2. You find it.
  3. You click it and reload/changes start to happen
  4. You enter another loop and find the element. Notice that this even can happen really fast as there is no wait after click, and thus find can exit giving you ID=1 element again. You try to click the element ID=1, but since it was reloaded, it does not exist anymore.

You can fix this in different ways:

  1. If speed is not an issue, you can add an explicit wait after click of a few seconds, giving enough time to javascript to finish.
  2. You can save the ID of the element, and every time you look for it, you check that is different, and if it is not, you wait and retry.

Giving this, if that is not your problem, you can always share more of your code and your testing target and I would be happy to help.


Post a Comment for "Python Selenium WebDriverWait And Click Inconsistently Giving StaleElementReferenceException()"