前言

selenium定位一组元素,批量操作循环点击的时候会报错:Element not found in the cache - perhaps the page has changed since it was looked up

实现目标:批量点击标题,获取每个页面的url地址

代码如下:

# coding:utf-8

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://www.cnblogs.com/yoyoketang/") all = driver.find_elements_by_css_selector(".postTitle2") for i in all:
i.click()
print(driver.current_url) # 打印当前页url
driver.back()

运行结果:

http://www.cnblogs.com/yoyoketang/p/7259993.html

Traceback (most recent call last):

selenium.common.exceptions.StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up

这里不少人就会问了:

  • “为什么第一次点击可以,for循环第二次点击就不行了呢?”

由于第一次点击后,页面刷新了,我们可以手工点击的时候,注意观察页面,页面是有刷新动作的。

  • “为什么明明定位到了,点击会报错呢?”

页面刷新后元素的属性是没变,但是element却变了,所有之前定位的元素element都过期了。

  • “那么如何实现呢?”

如何实现,这个才是本篇重点要讲的。

分析问题

1.当页面上有点击行为的时候,页面是会刷新的,为了模拟页面刷新后查看元素是不是会变,我们可以用refresh刷新页面,然后查看刷新前后元素的变化。

# coding:utf-8

from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get("https://www.cnblogs.com/yoyoketang/") all = driver.find_elements_by_css_selector(".postTitle2")
print(all) # 刷新前 driver.refresh()
all_new = driver.find_elements_by_css_selector(".postTitle2")
print(all_new) # 刷新后

运行结果:

[<selenium.webdriver.remote.webelement.WebElement (session="36801e98-3a57-41b1-a58e-021fe925fd57", element="{88a2f797-3833-4ea4-a734-72c5c59800ff}")>, <selenium.webdriver.remote.webelement.WebElement (session="36801e98-3a57-41b1-a58e-021fe925fd57", element="{529248de-6ca0-43d9-8747-34d7dad28c6c}")>,

...后面太长省略了]

2.很明显element里面的值发生了变化,所以第一次点击是可以点的,点完之后,页面刷新了,然后页面上的元素已经发生变化了,第二次循环的时候还是用刷新前的元素去定位点击的,自然就会报错了。

解决方案

1.针对页面刷新后,之前的元素失效问题,在for循环体里面可以重新定位一次,覆盖掉之前旧的就行了。

2.第一次获取全部元素后,通过len函数获取总个数

3.for循环的时候不要循环定位元素的list对象,换成range函数去循环

4.参考代码如下:

# coding:utf-8

from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get("https://www.cnblogs.com/yoyoketang/") all = driver.find_elements_by_css_selector(".postTitle2")
s = len(all)
print(u"获取总个数:%s"%s) for i in range(s):
all[i].click()
time.sleep(2)
url = driver.current_url
print(u"获取当前页面url:%s"%url)
driver.back() # 点完之后返回
# 重新获取一次元素
all = driver.find_elements_by_css_selector(".postTitle2")

运行结果:

selenium+python自动化88-批量操作循环点击报错:Element not found in the cache - perhaps the page has changed since it was looked up的更多相关文章

  1. Webdriver如何解决页面元素过期:org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up

    当运行Webdriver时抛出如下异常:org.openqa.selenium.StaleElementReferenceException: Element not found in the cac ...

  2. selenium Element not found in the cache - perhaps the page has changed since it was looked up接解决

    selenium Element not found in the cache - perhaps the page has changed since it was looked up.这个问题爆出 ...

  3. python自动化Traceback (most recent call last):报错

    今天使用python.然而遇见了Traceback (most recent call last):的报错.抓狂的一笔.有说path写错的,有说是...网上查到的资料也是很少.后来突然发现,页面上我暂 ...

  4. java selenium后报错Element not found in the cache元素定位要重新赋值之前的定义

    习惯上把定位的元素在操作之前就定位好, 例如: WebElement element1=driver.findElement(...);      ----------declaration1 Web ...

  5. selenium+python自动化98--文件下载弹窗处理(PyKeyboard)

    前言 在web自动化下载操作时,有时候会弹出下载框,这种下载框不属于web的页面,是没办法去定位的(有些同学一说到点击,脑袋里面就是定位!定位!定位!) 有时候我们并不是非要去定位到这个按钮再去点击, ...

  6. selenium+python自动化79-文件下载(SendKeys)

    前言 文件下载时候会弹出一个下载选项框,这个弹框是定位不到的,有些元素注定定位不到也没关系,就当没有鼠标,我们可以通过键盘的快捷键完成操作. SendKeys库是专业的处理键盘事件的,所以这里需要用S ...

  7. appium 与 selenium python解决python 'WebElement' object does not support indexing 报错问题问题

    再用selenium编写测试脚本时,发现出现python 'WebElement' object does not support indexing 报错问题问题,再找一些解决方法时,发现Appium ...

  8. python 3.5.2安装mysql驱动报错

    python 3.5.2安装mysql驱动报错 python 3.5.2安装mysql驱动时出现如下异常: [root@localhost www]# pip install mysql-connec ...

  9. selenium+python自动化81-html报告优化(饼图+失败重跑+兼容python2&3)

    优化html报告 为了满足小伙伴的各种变态需求,为了装逼提升逼格,为了让报告更加高大上,测试报告做了以下优化: 测试报告中文显示,优化一些断言失败正文乱码问题 新增错误和失败截图,展示到html报告里 ...

随机推荐

  1. HDU 6186 CS Course 前缀和,后缀和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6186 题意:给了n个数,然后有q个查询,每个查询要求我们删掉一个数,问删掉这个数后整个序列的与值,或值 ...

  2. 关闭自动弹出照片自动弹出iTunes以及关闭手机照片流

    关闭自动弹出照片自动弹出iTunes以及关闭手机照片流 如何阻止iPhone连接Mac后自动弹出照片? 时间:2015/6/18 17:07:15来源:本站原创作者:Chenjh我要评论 很多新 iP ...

  3. myeclipse创建的项目发布不了文档

    进入MyEclipse的工作目录下/.metadata/.plugins/org.eclipse.core.runtime/.settings/com.genuitec.eclipse.ast.dep ...

  4. 1.Python3标准库--前戏

    Python有一个很大的优势便是在于其拥有丰富的第三方库,可以解决很多很多问题.其实Python的标准库也是非常丰富的,今后我将介绍一下Python的标准库. 这个教程使用的书籍就叫做<Pyth ...

  5. django自带的django.core.mail模块实现发邮件的功能

    django自带了一个模块,可以实现发邮件的功能.如果项目开发遇到需要发邮件进行验证的时候可以用到. 1.先要准备发件人 发邮件需要使用SMTP.SMTP是什么呢? 简单邮件传输协议(Simple M ...

  6. Nginx web proxy NFS服务

    1.nginx web 安装 配置 #systemctl stop firewalld #systemctl disabled firewalld #wget -O /etc/yum.repos.d/ ...

  7. Flask 的系统学习

    详细看地址: http://www.cnblogs.com/wupeiqi/articles/7552008.html 一. 说明 Flask是一个基于Python开发并且依赖jinja2模板和Wer ...

  8. HTTP资源合集

    (1)MoZILLA开发者web技术文档之HTTP 未完待续...

  9. PHP需要学习成长路径

    第一阶段:基础阶段(基础PHP程序员) 重点:把LNMP搞熟练(核心是安装配置基本操作) 目标:能够完成基本的LNMP系统安装,简单配置维护;能够做基本的简单系统的PHP开发;能够在PHP中型系统中支 ...

  10. 微软企业库5.0 学习之路——第五步、介绍EntLib.Validation模块信息、验证器的实现层级及内置的各种验证器的使用方法——下篇

    一.独立验证器 我上篇中我将AndCompositeValidator和OrCompositeValidator归为独立验证器,这2个验证器主要是为了第一类验证服务,可以进行多种验证组合在一起进行复杂 ...