Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: element is not attached to the page document

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" />
like image 945
Sunstro Avatar asked May 22 '26 13:05

Sunstro


2 Answers

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
like image 100
bhupathi turaga Avatar answered May 24 '26 03:05

bhupathi turaga


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

like image 32
Krishna Kokila Avatar answered May 24 '26 02:05

Krishna Kokila



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!