stale element reference: element is not attached to the page document 错误

参考博客
https://www.jianshu.com/p/32e9442cf9c8

https://www.cnblogs.com/fengpingfan/p/4583325.html

官方文档中的解释:
https://selenium-python-zh.readthedocs.io/en/latest/api.html#module-selenium.common.exceptions

  • 这个错误的意思是:如果你之前定位了某个元素,在之后页面刷新后,这个元素需要重新获取否则可能报错
  • 这个错误并不是每次都会发生,所以可以捕获,然后在捕获代码重新调用**“指定的方法”**

下面代码在 12 行可能出错,所以在 except 中继续调用该方法

def next_page(page_num):

    try:
        next_page_btn = wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR,'#J_bottomPage > span.p-num > a.pn-next > em'))
        )

        time.sleep(2)           # 暂定两秒 
        # 点击下一页
        next_page_btn = driver.find_element(By.CSS_SELECTOR,'#J_bottomPage > span.p-num > a.pn-next > em')

        next_page_btn.click()				# 可能出错的位置 
    except StaleElementReferenceException:
        next_page(page_num)
  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值