I am getting the following error while using Selenium in python:
selenium.common.exceptions.StaleElementReferenceException: Message: u'stale element reference: element is not attached to the page document\n
Interestingly enough, the error pops up at different times in the for loop. Sometimes it gets through eg. 4 iterations and other times eg. 7.
Some of the relevant code being run is:
for i in range(0, 22):
u = driver.find_elements_by_id("data")
text = u[0].get_attribute("innerHTML")
driver.find_elements_by_class_name("aclassname")[0].click()
What does this error mean and what is something I can try to fix this?
Note: Python 3+ : update exceptions.StaleElementReferenceException,e -> exceptions.StaleElementReferenceException as e
Selenium Support
Explicit
andImplicit
Waits. If you think waiting for certain amount of time is enough for your page to be loaded, use:but if you want to wait for a special event (e.g. waiting for a particular element to be loaded) you can do something like:
It means the element is no longer in the DOM, or it changed.
You are inside a for loop. Think what happens during the iterations:
The following code may help you find the element:
I Would like to add one more solution here which is worked for me.
I was trying to access the button in the top menu panel on my webpage after refreshing the content area on the same page, Which gave me the following error,
Then I started to search for the solution to click the stale element on the web page. After two days of thinking and googling, I got a solution.
To access the stale element on the page, first, we need to focus the mouse over the particular section and perform click option
move_to_element will move the mouse over the stale element which we need to access once we got our control on the element the click operation is successfully performed.
This is worked for me. If anyone finds it working please comment your's as well which will help some other in future.
Thank you
Beyond the answers here, if you are using ActionChains, and the page has changed, be sure to reinstantiate your ActionChains object (dont reuse an old one), otherwise your ActionChain will be using a stale DOM. I.e. do this;
Or better yet dont use an instantiation;
This is the python solution for this problem: