selenium报错:selenium.common.exceptions.StaleElementReferenceException: Message: stale element refe...

1. 错误详情:

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



这是因为页面没有加载完,找不到元素节点

2. 解决方法:

如下代码所示,就是让浏览器显式等待,比如某个页面最下方有个跳页框,且类名为'ListFooter',那么下面的代码语义就是让浏览器加载并等待10秒,直到加载出类名为'ListFooter'的跳页框出现就不再等待,因为跳页框一般都放在网页最下面,这个元素加载到,那么就认为整个页面已经加载完

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By


wait = WebDriverWait(browser, 10)
wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'ListFooter')))
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容