Stale Exception Web Scraping Selenium Python
I am trying to grab information from tripadvisor. I sometimes get Message: stale element reference: element is not attached to the page document (Session info: chrome=47.0.2526
Solution 1:
A Stale Element Reference Exception occurs when an element:
- Has been deleted
- Is no longer attached to the DOM (as in your case)
- Has changed
From the docs:
You should discard the current reference you hold and replace it, possibly by locating the element again once it is attached to the DOM.
i.e.: "Find" the element again.
You'll need to modify the code to catch this error for the appropriate step.
from selenium.common.exceptions import StaleElementReferenceException
elem = driver.find_element_by_xpath('something leaves dom')
# ... do other actions which change the page and then later...try:
elem.click()
except StaleElementReferenceException:
elem = driver.find_element_by_xpath('something leaves dom')
elem.click()
Make a re-usable a version if you need it extensively for several elements.
Btw, you should not be catching Exception
in your code. Be specific about which ones you want to handle.
Post a Comment for "Stale Exception Web Scraping Selenium Python"