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处理的更多相关文章

  1. 报错stale element reference: element is not attached to the page document结局方案

    今天在调试脚本时,遇到如下报错: org.openqa.selenium.StaleElementReferenceException: stale element reference: elemen ...

  2. selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

    抓取网页代码后,由于是在同一个li标签下,所以使用一次性抓取,所有的a标签,然后循环做不同的操作,但是抛出找不到元素异常. def office_page(_chrome: Chrome): sn = ...

  3. 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 ...

  4. 关于xpath语句完全正确,但是页面报错: no such element: Unable to locate element: {"method":"xpath","selector":"xpath"}

    之前使用selenium-webdriver来写UI的自动化脚本,发现有一个元素一直无法定位,查看其源码,如下 利用xpathChecker验证了xpath语句的是正确的,但是控制台一直报错: no ...

  5. stale element reference: element is not attached to the page document 异常

    在执行脚本时,有时候引用一些元素对象会抛出如下异常 org.openqa.selenium.StaleElementReferenceException: stale element referenc ...

  6. java报错:The reference to entity "characterEncoding" must end with the ';' delimiter.

    java关于报错:The reference to entity "characterEncoding" must end with the ';' delimiter. Java ...

  7. 【问题记录】element is not attached to the page document

    遇到ui脚本报错:element is not attached to the page document 解决办法,再次定位即可

  8. 记录微信小程序报错 Unexpected end of JSON input;at pages/flow/checkout page getOrderData function

    微信小程序报错 Unexpected end of JSON input;at pages/flow/checkout page getOrderData function 这个报错是在将数组对象通过 ...

  9. Selenium2学习-012-WebUI自动化实战实例-010-解决元素失效:StaleElementReferenceException: stale element reference: element is not attached to the page document

    元素失效的想象提示信息如下图所示,此种问题通常是因为元素页面刷新之后,为重新获取元素导致的. 解决此类问题比较简单,只需要在页面刷新之后,重新获取一下元素,就可以消除此种错误了. 以下以易迅网搜索为例 ...

随机推荐

  1. 洛谷P3230 比赛

    emmmmmm,这个之前讲课的原题居然出到比赛里了. 我怒肝2h+然后A了此题,结果还是被某高一巨佬吊打...... 题意:n个球队两两比赛,胜得3分,败得0分,平得1分. 现有一个总分表,求问可能的 ...

  2. 【codevs4829】数字三角形++

    题目大意:给定一个数字三角形,求从 (1,1) 到第 N 行的路径经过的权值之和加上该路径上任意一个点的权值之和的最大值. 题解:任意加一条路径上的某个值,可以看成是多了一次选择的权利,即:在每次经过 ...

  3. 笔记: c开发gui程序 (WM_CREATE, WS_CLIPCHILDREN , SetWindowPos)

    过去两年,用c写的gui程序我一般使用的套路是: 在 winMain()中, 先创建一个主窗口, 紧接着就是在下面创建子窗口(子控件). 可能是因为写这方面的程序较少,所以也没遇到什么大问题,之前就是 ...

  4. bug6 项目检出JRE问题(Unbound classpath container: 'JRE System Library [JavaSE-1.7]' in project 'idweb')

    项目从SVN检出到工作空间后报了很多错误,其中很明显就是一些jar的问题,没有相关的jar或版本问题,看到最后的错误Unbound classpath Container: 'JRE System L ...

  5. Linux:echo中,>和>>的区别(保存结果和追加结果)

    在Linux中,对于echo命令,保存文件时,">"和">>"是有区别的: 假如有A_R1,B_R2,C_R1三个字符 for i in `l ...

  6. Dubbo 服务治理-mock实例

    转: Dubbo 服务治理-mock实例 老生住长亭 2017.02.28 10:56* 字数 514 阅读 2552评论 10喜欢 2 Dubbo的mock自己折腾的实例,配置信息有点简陋,有点粗鄙 ...

  7. shiro+SpringMVC 项目 配置404页面

    说的配置404,大家都会想到去web.xml里面配置 <error-page> <error-code></error-code> <location> ...

  8. KindEditor 4.1.2版本,在上传图片的时候 设置为绝对路径

    被这个问题困扰了很久,最终搞定,竟然是如此之简单.   方法如下: 1.打开编辑器目录下的:kindeditor.js 2.修改:urlType : ”, 为:urlType : ‘domain’, ...

  9. html实现猜数字游戏

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. 多线程Java Socket编程

    采用Java 5的ExecutorService来进行线程池的方式实现多线程,模拟客户端多用户向同一服务器端发送请求. 1.服务端 package localSocket; import java.i ...