Jump to content

Python Selenium - element is not attached to the page document - once button is clicked

Featured Replies

When I click on an item inside a dropdown list, the item is correctly clicked but the error: "stale element is not attached to the page document" is raised.

I guess this error is raised because of an element I use as changed inside the DOM or has been refreshed, but I didn't find any solutions for now. Here's my code:

exam_question_type = self.driver.find_element(By.XPATH, f"//*[name()='svg'][@class='css-19bqh2r']")
exam_question_type.click()
WebDriverWait(self.driver).until(ec.visibility_of_element_located((By.CSS_SELECTOR, 'class=" css-b02ejv-menu"'))
select_question_type = self.driver.find_elements(By.CSS_SELECTOR, 'class="CustomOptionMenu_button__2GnMd"')
for type in select_question_type:
    if type.text == "QCM grid":
        try:
            ActionChains(self.driver).click(type).perform()
        except StaleElementReferenceException:
            pass

What i'm doing wrong?

In the thread https://stackoverflow.com/questions/12967541/how-to-avoid-staleelementreferenceexception-in-selenium

"This can happen if a DOM operation happening on the page is temporarily causing the element to be inaccessible. To allow for those cases, you can try to access the element several times in a loop before finally throwing an exception."

"Generally this is due to the DOM being updated and you trying to access an updated/new element -- but the DOM's refreshed so it's an invalid reference you have.."

 

Please sign in to comment

You will be able to leave a comment after signing in

Sign In Now

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.