selenium.common.exceptions.StaleElementReferenceException 报错原因

原因是点击第二个时已经是新页面,找不到之前页面的元素

 

报错代码段

elem_pic = driver.find_elements_by_xpath("//div[@class='imgpage']/ul/li/div/a/img")
for i in elem_pic:
    elem_url = i.get_attribute("src")
    driver.get(elem_url)
    urlretrieve(elem_url,"m"+str(c)+".jpg")

报错:

selenium.common.exceptions.StaleElementReferenceException: Message: {"errorMessage":"Element does not exist in cache","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:50811","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"GET","url":"/attribute/src","urlParsed":{"anchor":"","query":"","file":"src","directory":"/attribute/","path":"/attribute/src","relative":"/attribute/src","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/attribute/src","queryKey":{},"chunks":["attribute","src"]},"urlOriginal":"/session/e23a6270-ad9c-11e7-9d81-eb604b74f70b/element/:wdc:1507627515312/attribute/src"}}

 

只要刷新页面之后重新获取元素就行,不要提前获取一组元素,然后去循环操作每一个元素,这种情况还是获取元素的个数,然后在循环中获取相应位置的元素,在用的时候才去获取

遇到页面有变化的情况,不要去循环元素,去循环个数或者定位方式,在循环中获取元素

转载于:https://www.cnblogs.com/AbsolutelyPerfect/p/7647158.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值