在做自动化测试的时候,遇到此问题多次:
StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
原因分析:
就是抓取元素的时候,突然间 网页刷新,导致元素对象不是刚才的那一个。
解决方案:
1)抓取错误,再次抓取对象
try:
element = driver.find_element_by_id("button")
except StaleElementReferenceException :
element = driver.find_element_by_id("button") # 出现错误,再次抓取
2)重新刷新:
driver.refresh() # 场景有限
3)设置时间
time.sleep() # 场景有限