Python: element is not attached to the page document

16,328

Solution 1

try:
   driver.get("http://www.supremenewyork.com/shop/shirts/m2wvkj5u6")
    time.sleep(3)
   driver.find_element_by_css_selector("#add-remove-buttons > input").click()
except:
    pass

Solution 2

First of all you get Stale Element Exception when the properties of the element which you are trying to perform an operation on has changed.

This can happen when there is a difference in time from when you find the element to performing the operation.

try using Xpath instead of CSS

driver.findElement(By.Xpath("//input[.='add to cart']")).click();

Let me know if it works

Share:
16,328

Related videos on Youtube

Sunstro
Author by

Sunstro

Updated on June 04, 2022

Comments

  • Sunstro
    Sunstro almost 2 years

    I keep getting this error, and while I fixed it for another element, I can't fix it for this one. I think it's because for the other element I could find_by_ID, whereas this element does not have an ID.

    while True:
    try:
         driver.find_element_by_name('commit')
         break
    except (NoSuchElementException, StaleElementReferenceException):
         time.sleep(1)
         wait=WebDriverWait(driver, 10,ignored_exceptions=ignored_exceptions).until(EC.presence_of_element_located((By.NAME, 'commit')))
    driver.find_element_by_css_selector('input.button').click()
    

    Error:

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

    Reference page: http://www.supremenewyork.com/shop/shirts/go8jt7kse/f74y2ihpz

    Specific HTML element:

    <input type="submit" name="commit" value="add to cart" class="button" />
    
    • undetected Selenium
      undetected Selenium almost 6 years
    • Pradeep hebbar
      Pradeep hebbar almost 6 years
      driver.find_element_by_name('commit') , this line of code working for me with out any exception.
    • Ratmir Asanov
      Ratmir Asanov almost 6 years
      Did you try my answer?
  • Sunstro
    Sunstro almost 6 years
    It still gives me: selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document