12

I have a python function that should click through all options of a product:

submit_button = driver.find_element_by_id('quantityactionbox')
elementList = submit_button.find_elements_by_tag_name("option")
for x in elementList:
 x.click()

After I clicked 2 elements I get this error:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

Can you maybe tell me why this error appaer and what can I do to go successfully through all elements?

asked Mar 15, 2016 at 13:47

4 Answers 4

13

You have the explanation and the solution on The Element is not Attached to the DOM:

A common technique used for simulating a tabbed UI in a web app is to prepare DIVs for each tab, but only attach one at a time, storing the rest in variables. In this case, it's entirely possible that your code might have a reference to an element that is no longer attached to the DOM (that is, that has an ancestor which is "document.documentElement").

If WebDriver throws a stale element exception in this case, even though the element still exists, the reference is lost. You should discard the current reference you hold and replace it, possibly by locating the element again once it is attached to the DOM.

answered Mar 15, 2016 at 14:03
Sign up to request clarification or add additional context in comments.

Comments

13

The easy way to overcome many of these types of errors is to just add some sort of delay:

import time
time.sleep(1) 

DOM manipulation after an event is fired usually takes a bit of time so you're not really losing that much of performance.

answered Dec 18, 2019 at 1:53

1 Comment

Sometimes it really is that simple! Thanks!
1

In my case it was because the page had changed and the element no longer existed but my script was trying to call it. It was not readily obvious because the page did have essentially the same element but it had been reloaded and was therefore, not the exact same element and not available in the current page document. I had to redefine the element after the page was reloaded.

answered Aug 12, 2021 at 19:05

1 Comment

also for example if something is added to the current page, for example a error message or something, basically the page is adding a new element to the html tree, so the path to the element is lost by selenium, so you have to find again the element after interactuate with the element
0

In my case, chrome was updated and I was using older 'chrome driver.exe'. Please check version info.

answered Jan 23, 2024 at 7:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.