selenium之坑:点击后页面刷新重新获取刷新前的页面(StaleElementReferenceException:Message:Element not found in the cache...)
循环点击一列链接,但只能点到第一个,第二个失败,这是为什么,因为第二个已经是新页面,当然找不到之前页面的元素,虽然元素没变,甚至是后退回来,页面都没有变,为什么是新页面,页面长的一样不代表是同一张页面,就像两个人长得一样不一定是同一个人,他们身份证号不同,页面,页面上的元素都是有自己的身份证号的
#coding:utf8
from selenium import webdriver
driver=webdriver.Chome()
driver.get(url)
print(driver.find_element_by_id('kw'))
driver.refresh()
print(driver.find_element_by_id('kw'))
driver.quit()
结果:
<selenium.webdriver.remote.webelement.WebElement (session="d726e8fdfbc05f9bea26e33d09a2a67f", element="0.6155236891000833-1")>
<selenium.webdriver.remote.webelement.WebElement (session="d726e8fdfbc05f9bea26e33d09a2a67f", element="0.3351209127484813-1")>
我们发现,仅仅是刷新了一下页面,两次的element_id是不同的,也就是说这是两个不同的元素,如果你用以下的方式来定位,自然会因为找不到而报错
#coding:utf8 from selenium import webdriver driver=webdriver.Chrome()
url='http://www.baidu.com'
driver.get(url) kw=driver.find_element_by_id('kw')
kw.click() driver.refresh() kw.click()
driver.quit() 结果:
Traceback (most recent call last):
File "D:/py3code/xxx/last_week/test.py", line 16, in <module>
kw.click()
File "D:\py3.6\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "D:\py3.6\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "D:\py3.6\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "D:\py3.6\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=68.0.3440.106)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.1.7601 SP1 x86_64)
原因很明显,你用别人的身份证id去找现在的人,哪怕这两个人长的很像,他也会告诉你,对不起,你找错人啦
当然,不仅仅这一种情况,如果你执行以下的操作,都有可能找错人:
refresh,不论你是主动刷新还是自动刷新
back,已经跳转到了其他页面,然后你用driver.back()跳回来,这也是一张新的页面了
跳转到了新的页面,但这张新的页面上有一些元素跟之前页面是长的一样的,这也是一张新的页面了。比如:一排分页按钮,你点击下一页跳转到了第二页,想要还用原来的元素操作到下一页,那也是不可能的了
除此之外可能还有其他的原因,总之你看到这类型长的差不多,但是对页面有了操作的情况,就应该想想这种可能性了
遇到这种情况该肿么办?
很简单:
只要刷新页面后重新获取元素就行,不要提前获取一组元素,然后去循环操作每一个元素,这种情况还是获取元素的个数,然后再循环中获取相应位置的元素,在用的时候才去获取,这样你就获取到最新的id了,也不会出现找错人的尴尬了
总之一句话,遇到页面有变化的情况,不要去循环元素,去循环个数或者定位方式,再循环中获取元素
解决方法示例:
错误写法:
all_a=driver.find_elements_by_class('class_name')
for a in all_a:
a.click()
这样就容易点击了第一个a之后,页面出现刷新的情况,再想点第二个就会报这个错
可以改成:
counts_a=len(driver.find_elements_by_class('class_name'))
for i in range(counts_a):
driver.find_elements_by_xpath('//a[@class="class_name"][i+1]').click()
可能会更好一些,当然,也有其他的写法,大概意思就是需要在刷新后重新去定位一次,再用重新定位到的元素去操作
selenium之坑:点击后页面刷新重新获取刷新前的页面(StaleElementReferenceException:Message:Element not found in the cache...)的更多相关文章
- selenium之 坑(StaleElementReferenceException: Message: Element not found in the cache...)
今天给大家分享一个selenium中经常会有人遇到的坑: selenium.common.exceptions.StaleElementReferenceException: Message: Ele ...
- selenium之坑(StaleElementReferenceException: Message: Element not found in the cache...)
有时候循环点击一列链接,只能点到第一个,第二个就失败了 原因是第二个已经是新页面,当然找不到之前页面的元素.就算是后退回来的,页面也是不一样的 页面长的一样不一定是同一张页面,就像两个人长的一样不一定 ...
- yii2获取登录前的页面url地址--电脑和微信浏览器上的实现以及yii2相关源码的学习
对于一个有登录限制(权限限制)的网站,用户输入身份验证信息以后,验证成功后跳转到登录前的页面是一项很人性化的功能.那么获取登录前的页面地址就很关键,今天在做一个yii2项目的登录调试时发现了一些很有意 ...
- 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 ...
- Python3 Selenium WebDriver网页的前进、后退、刷新、最大化、获取窗口位置、设置窗口大小、获取页面title、获取网页源码、获取Url等基本操作
Python3 Selenium WebDriver网页的前进.后退.刷新.最大化.获取窗口位置.设置窗口大小.获取页面title.获取网页源码.获取Url等基本操作 通过selenium webdr ...
- Selenium WebDriver-网页的前进、后退、刷新、最大化、获取窗口位置、设置窗口大小、获取页面title、获取网页源码、获取Url等基本操作
通过selenium webdriver操作网页前进.后退.刷新.最大化.获取窗口位置.设置窗口大小.获取页面title.获取网页源码.获取Url等基本操作 from selenium import ...
- selenium常用命令--操作页面元素及获取元素内容整理
selenium常用命令之操作页面元素及获取元素内容的事件整理 例子: /**id <input type="text" id="phone" name ...
- Python3.x:selenium获取iframe内嵌页面的源码
Python3.x:selenium获取iframe内嵌页面的源码 前言 在一些网页中经常会看到ifrmae/frame标签,iframe是嵌入式框架一般用来在已有的页面中嵌入另一个页面,当一个元素在 ...
- Python+Selenium之断言对应的元素是否获取以及基础知识回顾
# coding=utf-8 from selenium import webdriver driver = webdriver.Firefox() driver.maximize_window () ...
随机推荐
- 关于extern的说明
extern的作用: extern可置于变量或者函数前,以表示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其定义. 声明外部变量 现代编译器一般采用按文件编译的方式 ...
- Ubuntu安装Sun JDK
Ubuntu 14.04 下安装 Sun JDK 1.8.0 1.下载JDK http://www.oracle.com/technetwork/java/javase/downloads/jdk8- ...
- Linux 部署jenkins + svn + mavne + tomcat8自动化部署
1,下载Jenkins war包 官网下载地址 https://jenkins.io/ 2.初始化安装 1)首次启动jenkins,出于安全考虑,jenkins会生成一个随机的口令到 /root ...
- oracle 数据查询
1,读取从今天到1个月前之间的数据select * from tablewhere column between add_months(sysdate, -1) and sysdate;
- 随机生成数,摘自算法竞赛入门经典P120-P123测试STL。
//#include<bits/stdc++.h> #include<cstring> #include<iostream> #include<cstdio& ...
- C# winform中 选择文件和保存文件
转载自https://blog.csdn.net/qq_31788297/article/details/62047952 我们在使用桌面软件的时候经常会使用到选择文件并打开和另存为等的窗口,这样方便 ...
- 远程连接windows时剪贴板失效解决方法
1:打开任务管理器2:找到结束进程rdpclip,找不到可以不管.3:手工新建任务里输入rdpclip,运行即可.
- C字符串复制
void mystrcpy(char *from, char *to) { for(; *from != '\0'; from++, to++) { *to = *from; } *to = '\0' ...
- js创建对象的最佳方式
1.对象的定义 ECMAScript中,对象是一个无序属性集,这里的“属性”可以是基本值.对象或者函数 2.数据属性与访问器属性 数据属性即有值的属性,可以设置属性只读.不可删除.不可枚举等等 访问器 ...
- 表变量、临时表(with as ,create table)
1.declare @t table(CountryRegionCode nvarchar(3))insert into @t(CountryRegionCode) (select CountryR ...