3

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" />
Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
Sunstro
  • 75
  • 2
  • 4
  • 9

3 Answers3

4

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
bhupathi turaga
  • 297
  • 2
  • 16
1

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

0

It works

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
bhupathi turaga
  • 297
  • 2
  • 16
  • It still gives me: selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document – Sunstro May 06 '18 at 17:33