关于报错stale element reference: element is not attached to the page document处理
1、现象
在执行脚本时,有时候引用一些元素对象会抛出如下异常
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
2、报错原因
官方给出解释如下:
The element has been deleted entirely.
The element is no longer attached to the DOM.
我个人理解:
就是页面元素过期,引用的元素过时,不再依附于当前页面,需要重新定位获取元素对象
如果JavaScript把网页给刷新了,那么操作的时候就会碰到Stale Element Reference Exception。
官方地址:http://docs.seleniumhq.org/exceptions/stale_element_reference.jsp
解决方案:
使用WebDriverWait类的构造方法接受了一个WebDriver对象和一个等待最长时间(30秒)。然后调用until方法,其中重写了ExpectedCondition接口中的apply方法,让其返回一个WebElement,即加载完成的元素,然后点击。默认情况下,WebDriverWait每500毫秒调用一次ExpectedCondition,直到有成功的返回,当然如果超过设定的值还没有成功的返回,将抛出异常,循环五次查找,实际实验发现,函数里虽然最多尝试5次,但是一般也就查询最多3次,也在没有报错,比起线程等待要好很多,
代码如下:
/**
* 等待指定元素文本出现
*
* @param xpath
* @param text
* @return
* @throws Exception
*/
public Boolean isDisplay(final String xpath, final String text) {
logger.info("等待指定元素文本显示");
boolean result = false;
int attempts = 0;
while (attempts < 5) {
try {
attempts++;
logger.info("扫描开始元素开始第" + attempts + "次");
result = new WebDriverWait(driver, 30)
.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return driver.findElement(By.xpath(xpath)).getText().contains(text);
}
});
logger.info("扫描开始元素结束");
return true;
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
关于报错stale element reference: element is not attached to the page document处理的更多相关文章
- 报错stale element reference: element is not attached to the page document结局方案
今天在调试脚本时,遇到如下报错: org.openqa.selenium.StaleElementReferenceException: stale element reference: elemen ...
- selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
抓取网页代码后,由于是在同一个li标签下,所以使用一次性抓取,所有的a标签,然后循环做不同的操作,但是抛出找不到元素异常. def office_page(_chrome: Chrome): sn = ...
- mybatis项目启动报错 The content of element type "resultMap" must match "(constructor?,id*,result*,association*,collection*,discriminator?)".
启动项目报错 2018-02-26 17:09:51,535 ERROR [org.springframework.web.context.ContextLoader] - Context initi ...
- 关于xpath语句完全正确,但是页面报错: no such element: Unable to locate element: {"method":"xpath","selector":"xpath"}
之前使用selenium-webdriver来写UI的自动化脚本,发现有一个元素一直无法定位,查看其源码,如下 利用xpathChecker验证了xpath语句的是正确的,但是控制台一直报错: no ...
- stale element reference: element is not attached to the page document 异常
在执行脚本时,有时候引用一些元素对象会抛出如下异常 org.openqa.selenium.StaleElementReferenceException: stale element referenc ...
- java报错:The reference to entity "characterEncoding" must end with the ';' delimiter.
java关于报错:The reference to entity "characterEncoding" must end with the ';' delimiter. Java ...
- 【问题记录】element is not attached to the page document
遇到ui脚本报错:element is not attached to the page document 解决办法,再次定位即可
- 记录微信小程序报错 Unexpected end of JSON input;at pages/flow/checkout page getOrderData function
微信小程序报错 Unexpected end of JSON input;at pages/flow/checkout page getOrderData function 这个报错是在将数组对象通过 ...
- Selenium2学习-012-WebUI自动化实战实例-010-解决元素失效:StaleElementReferenceException: stale element reference: element is not attached to the page document
元素失效的想象提示信息如下图所示,此种问题通常是因为元素页面刷新之后,为重新获取元素导致的. 解决此类问题比较简单,只需要在页面刷新之后,重新获取一下元素,就可以消除此种错误了. 以下以易迅网搜索为例 ...
随机推荐
- 11:SSM框架下各个层的解释说明
具体见网址:http://blog.csdn.net/lutianfeiml/article/details/51864160
- share.js一键分享到微博、QQ空间、QQ好友、微信、腾讯微博、豆瓣、Facebook、Twitter、Linkedin、Google+、点点等
官网上面 https://github.com/overtrue/share.js非常详细的介绍了share.js他的使用 使用方式有:第一:使用 npm npm install social-sha ...
- MATLAB:图像的移动(move函数)
图像移动涉及到move函数,实现过程如下: close all; %关闭当前所有图形窗口,清空工作空间变量,清除工作空间所有变量 clear all; clc; I=imread('lenna.bmp ...
- KindEditor 4.1.2版本,在上传图片的时候 设置为绝对路径
被这个问题困扰了很久,最终搞定,竟然是如此之简单. 方法如下: 1.打开编辑器目录下的:kindeditor.js 2.修改:urlType : ”, 为:urlType : ‘domain’, ...
- vs2010中使用 git
在没有使用git之前的,我很苦恼.因为我的代码有时在办公室做,有时也带回家做.做了一些时间,放在哪,要用的时间就不知道家里的还是办公室的是新版本了.甚至出现了旧版本把新版本覆盖的乌龙事情.有了git只 ...
- PHP自动加载(__autoload和spl_autoload_register)
一:什么是自动加载 我们在new出一个class的时候,不需要手动去require或include来导入这个class文件,而是程序自动帮你导入这个文件不需要手动的require那么多class文件了 ...
- java连接数据库读取数据出现乱码
因为这是通用编码,像中国通常使用的GBK.GB2312.Big5等只是针对中文而言,但是对其他文字就不适用了,为了使得这个问题的解决具有文字编码通用性,所以我这里设定了UTF8这个编码. 编码一致性涉 ...
- 附录A Spring Boot应用启动器
spring Boot应用启动器基本的一共有44种,具体如下: 1)spring-boot-starter 这是Spring Boot的核心启动器,包含了自动配置.日志和YAML. 2)spring- ...
- 版本管理——git
一.git简介 git是一个 分布式版本控制系统 ,学习git之前首先要了解什么是分布式版本控制系统. 集中式版本控制:版本库集中存放在中央服务器,我们工作的时候先从中央服务器获取最新版本,干完活后把 ...
- 006、容器 What、Why、How(2018-12-21 周五)
参考https://www.cnblogs.com/CloudMan6/p/6751516.html What - 什么是容器? 容器是一种轻量级.可移植.自包含的软件打包技术,是应用 ...