selenium+python自动化88-批量操作循环点击报错: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
实现目标:批量点击标题,获取每个页面的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的更多相关文章
- 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 ...
- 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.这个问题爆出 ...
- python自动化Traceback (most recent call last):报错
今天使用python.然而遇见了Traceback (most recent call last):的报错.抓狂的一笔.有说path写错的,有说是...网上查到的资料也是很少.后来突然发现,页面上我暂 ...
- java selenium后报错Element not found in the cache元素定位要重新赋值之前的定义
习惯上把定位的元素在操作之前就定位好, 例如: WebElement element1=driver.findElement(...); ----------declaration1 Web ...
- selenium+python自动化98--文件下载弹窗处理(PyKeyboard)
前言 在web自动化下载操作时,有时候会弹出下载框,这种下载框不属于web的页面,是没办法去定位的(有些同学一说到点击,脑袋里面就是定位!定位!定位!) 有时候我们并不是非要去定位到这个按钮再去点击, ...
- selenium+python自动化79-文件下载(SendKeys)
前言 文件下载时候会弹出一个下载选项框,这个弹框是定位不到的,有些元素注定定位不到也没关系,就当没有鼠标,我们可以通过键盘的快捷键完成操作. SendKeys库是专业的处理键盘事件的,所以这里需要用S ...
- appium 与 selenium python解决python 'WebElement' object does not support indexing 报错问题问题
再用selenium编写测试脚本时,发现出现python 'WebElement' object does not support indexing 报错问题问题,再找一些解决方法时,发现Appium ...
- python 3.5.2安装mysql驱动报错
python 3.5.2安装mysql驱动报错 python 3.5.2安装mysql驱动时出现如下异常: [root@localhost www]# pip install mysql-connec ...
- selenium+python自动化81-html报告优化(饼图+失败重跑+兼容python2&3)
优化html报告 为了满足小伙伴的各种变态需求,为了装逼提升逼格,为了让报告更加高大上,测试报告做了以下优化: 测试报告中文显示,优化一些断言失败正文乱码问题 新增错误和失败截图,展示到html报告里 ...
随机推荐
- FineReport——JS二次开发(分页预览)
BS访问某个cpt模板,报表servlet将会将cpt文件解析成对应的html,报表内容最终转换为一个table,位于id=content-container的div中. 在模板和html页面中,他们 ...
- POJ-3268
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13738 Accepted: 6195 ...
- SecureCrt的操持连接办法
保持连接: options -> global options -> General -> Default Session,点击Edit default settings按钮,在Te ...
- 兼容python3的SSDB客户端
SSDB.py import socket class SSDB_Response(object): def __init__(self, code='', data_or_message=None) ...
- 解决Gradle报错找不到org.gradle.api.internal.project.ProjectInternal.getPluginManager()方法问题
因为本地的AndroidStudio很久没用了,所以想要研究下github上的某个代码的时候,还得重新配下环境 打开了几个项目,都是提示如下错误 Error:Unable to find method ...
- react native android应用启动画面
参考地址:https://www.youtube.com/watch?v=rnLR65OGtic 第一步:生成启动画面的背景图片 生成一个2048*2048的背景图片,打开网站https://apet ...
- 转:libFuzzer Tutorial(libFuzzer教程)
转:https://github.com/google/fuzzer-test-suite/blob/master/tutorial/libFuzzerTutorial.md 本文在Ubuntu16下 ...
- HDU 4865 Peter's Hobby
$dp$. 这题的本质和求一个有向无环图的最长路径长度的路径是一样的. $dp[i][j]$表示到第$i$天,湿度为$a[i]$,是第$j$种天气的最大概率.记录一下最大概率是$i-1$天哪一种天气推 ...
- 洛谷——P1862 输油管道问题
P1862 输油管道问题 题目背景 听说最近石油危机 所以想到了这题 题目描述 某石油公司计划建造一条由东向西的主要输油管道.该管道要穿过一个有n口油井的油田.从每口油井都要有一条输油管道沿最短路径( ...
- js中ajax的异步性
最近项目里遇到ajax异步性的问题,简化后的代码如下: function ajaxGetEvents(calendarView, time) { var year = time.getFullYear ...