selenium之 坑(StaleElementReferenceException: Message: Element not found in the cache...)
今天给大家分享一个selenium中经常会有人遇到的坑:
selenium.common.exceptions.StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up
群里经常会有人问,“我循环去点击一列链接,但是只能点到第一个,第二个就失败了,为什么?”。原因就在这里:你点击第二个时已经是新页面,当然找不到之前页面的元素。这时,他会问“可是明明元素就在那里,没有变,甚至我是回退回来的,页面都没有变,怎么会说是新页面?”。这个就需要你明白页面长得一样不代表就是同一张页面,就像两个人长得一样不一定是同一个人,他们的身份证号不同。页面,甚至页面上的元素都是有自己的身份证号(id)的。
我们来试试看:
代码:
# -*- coding: utf-8 -*-
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.baidu.com')
print driver.find_element_by_id('kw') # kw before refresh
driver.refresh() # refresh
print driver.find_element_by_id('kw') # kw after refresh
driver.quit()
结果:
<selenium.webdriver.remote.webelement.WebElement (session="6c251157-6d81-435c-9100-97696a46ab9c", element="{f74ae41d-a557-4d5c-9029-3c122e3d0744}")>
<selenium.webdriver.remote.webelement.WebElement (session="6c251157-6d81-435c-9100-97696a46ab9c", element="{d7bd4320-31f2-4708-824f-f1a8dba3e79b}")>
我们发现,仅仅是刷新了一下页面,两次的element id是不同的,也就是说这是两个不同的元素,如果你用以下的方式来定位,自然会因为找不到而报错:
# -*- coding: utf-8 -*-
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.baidu.com')
kw = driver.find_element_by_id('kw') # 先定位并获得kw元素
kw.click()
driver.refresh() # refresh
kw.click() # 刷新后,仍用原来的kw元素操作,这时会报错
driver.quit()
结果:
Traceback (most recent call last):
File "D:/Code/py/AutoTestFramework/src/others/tryrefreshpage.py", line 16, in <module>
kw.click()
File "C:\APP\Python2.7.10\lib\site-packages\selenium\webdriver\remote\webelement.py", line 75, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\APP\Python2.7.10\lib\site-packages\selenium\webdriver\remote\webelement.py", line 469, in _execute
return self._parent.execute(command, params)
File "C:\APP\Python2.7.10\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "C:\APP\Python2.7.10\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up
Stacktrace:
....
原因很明显,你用别人的身份证id去找现在的人,哪怕这两个人长得很像,他也会告诉你:对不起,你找错人了。
当然,不仅仅这一种情况,如果你执行以下的操作,都有可能找错人:
- refresh,不论你是主动刷新还是页面自动刷新
- back,已经跳转到了其他页面,然后你用driver.back()跳回来,这也是一张新的页面了
- 跳转到了新的页面,但这张新页面上有一些元素跟之前页面是长得一样的,这也是一张新的页面了。比如:一排分页按钮,你点击下一页跳转到了第二页,想要还用原来的元素操作到下一页,那也是不可能的了。
除此之外可能还有其他的原因,总之你看到这类型长得差不多,但是对页面有了操作的情况,就应该想想这种可能性了。
那遇到这种情况该怎么办?
很简单:
只要刷新页面之后重新获取元素就行,不要提前获取一组元素,然后去循环操作每一个元素,这种情况还是获取元素的个数,然后在循环中获取相应位置的元素,在用的时候才去获取,这样你就获取到最新的id了,也不会出现找错人的尴尬了。
总之一句话,遇到页面有变化的情况,不要去循环元素,去循环个数或者定位方式,在循环中获取元素。
selenium之 坑(StaleElementReferenceException: Message: Element not found in the cache...)的更多相关文章
- selenium之坑:点击后页面刷新重新获取刷新前的页面(StaleElementReferenceException:Message:Element not found in the cache...)
循环点击一列链接,但只能点到第一个,第二个失败,这是为什么,因为第二个已经是新页面,当然找不到之前页面的元素,虽然元素没变,甚至是后退回来,页面都没有变,为什么是新页面,页面长的一样不代表是同一张页面 ...
- selenium之坑(StaleElementReferenceException: Message: Element not found in the cache...)
有时候循环点击一列链接,只能点到第一个,第二个就失败了 原因是第二个已经是新页面,当然找不到之前页面的元素.就算是后退回来的,页面也是不一样的 页面长的一样不一定是同一张页面,就像两个人长的一样不一定 ...
- selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
抓取网页代码后,由于是在同一个li标签下,所以使用一次性抓取,所有的a标签,然后循环做不同的操作,但是抛出找不到元素异常. def office_page(_chrome: Chrome): sn = ...
- 爬虫-Selenium -抱错ElementNotVisibleException: Message: element not visible
1.当使用Selenium IDE 完成了脚本的录制和回放通过后,想要将脚本转换为其他语言如java.Python等,首次使用时打开Options->Format发现没有可以转换的语言,如下: ...
- selenium.common.exceptions.ElementNotVisibleException: Message: element not visible处理方法:selenium针对下拉菜单事件的处理
使用Selenium爬虫时,可能会遇到一些下拉菜单,动态加载,如果直接使用find_element_by_函数会报错,显示selenium.common.exceptions.ElementNotVi ...
- python + web自动化,点击不生效,提示“selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (117, 674)”
前言: 在做web自动化时,遇到一个缩放了浏览器比例的操作,从100%缩小到80%,再进行点击的时候,弹出报错信息,无法点击 selenium.common.exceptions.ElementCli ...
- 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 ...
- 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 ...
- python+selenium:点击页面元素时报错:WebDriverException: Message: Element is not clickable at point (1372.5, 9.5). Other element would receive the click: <li style="display: list-item;" id="tuanbox"></li>
遇到一个非常郁闷的问题,终于解决了, 问题是这样的,NN网站的价格计划,每一个价格计划需要三连击才能全部点开,第一个房型的价格计划是可以正确三连击打开的,可是第二个房弄就不行了,报错说不是可点击的 ...
随机推荐
- 做自己的软件的Gallery(一)
先上效果图: 如图,android默认也有Gallery,很多软件在调用时,都是使用自己的Gallery,一方面好维护,另外一方面可以做优化.要做成以上样式,图片加载类起至关重要,一不小心,就好OOM ...
- C语言二维数组实现扫雷游戏
#include<stdio.h> //使用二维数组实现 扫雷 int main() { char ui[8][8]={ '+','+','+','+','+','+','+','+', ...
- 用shell脚本挂载linux主机拷贝相应文件copy.sh
#!/bin/sh # $1 MOUNTDIR $2 TARGETDIR $3 ERRORLOG #参数检查 if test $# -ne 3 then echo "argument che ...
- 恶补web之八:jQuery(3)
jquery和其他js框架.jQuery使用$作为jQuery的简写,但是还有很多js框架,比如: MooTools,Backbone,Sammy,Cappuccino,Knockout,JavaSc ...
- IoC和DI的基本概念的思维导图
最近在学习Spring开发,IoC这个概念让我有点儿迷糊,控制反转这四个字是在是无法做到望文生义,于是乎就找了一些材料来学习,研究了半天,绘制了下面这幅思维导图.仅供参考!
- Oracle——多表查询
本次预计讲解的知识点 1. 多表查询的操作.限制.笛卡尔积的问题: 2. 统计函数及分组统计的操作: 3. 子查询的操作,并且结合限定查询.数据排序.多表查询.统计查询一起完成各个复杂查询的操作: 一 ...
- 新知识:JQuery语法基础与操作
jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设计的宗旨是"write ...
- MDCC2013会议笔记
技术性的Topic听的不多,也没记多少东西. 下面这些是产品设计论坛的笔记: 互联网为实体行业带来:数据驱动,用户参与,快速验证想法 体验整合:线上与线下,产品与服务,运营与营销,用户和利益相关方体验 ...
- View requires API level 14 (current min is 8): <GridLayout>
在学习android的过程中,出现这个错误的是否,可以build clean解决
- Java虚拟机-类加载
虚拟机把描述类的数据从Class文件加载到内存,并对数据进行检验.转换解析和初始化,最终形成了可以被虚拟机直接使用的Java类型,这就是虚拟机的类加载机制.在Java语言里,类型的加载.连接和初始化过 ...