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;}

转载于:https://blog.51cto.com/9360230/2069328

关于报错stale element reference: element is not attach相关推荐

  1. Selenium | 引用WebElement时报错stale element reference: element is not attached to the page document

    当项目中需要断言文本是否在元素列表的文本集中时,在对元素列表中的元素for循环遍历进行WebElement.text 操作时,随机性的出现state element reference的报错信息 源代 ...

  2. selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element

    在输入框输入内容,点击[查询],然后点击页面上的[处理],并进行点击时,报错,报错内容如下. selenium.common.exceptions.StaleElementReferenceExcep ...

  3. Message: stale element reference: element is not attached to the page document

    背景:获取一组按钮,数量不确定,得到所有按钮A之后使用get_attribute("innerHTML")获取xpath并用A.find_element来获取每个按钮 代码如下: ...

  4. [python] Message: stale element reference: element is not attach to the page document

    1.前言 通过selenium中find_elements_by_partial_link_text()函数获取网页中符合条件的链接文本列表,并挨个点击,出现了如下两种错误. 2.准备 2.1.手写H ...

  5. mybatis项目启动报错 The content of element type resultMap must match (constructor?,id*,result*,associat...

    启动项目报错 2018-02-26 17:09:51,535 ERROR [org.springframework.web.context.ContextLoader] - Context initi ...

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

    stale element reference: element is not attached to the page document 异常 参考文章: (1)stale element refe ...

  7. Vue报错:Unknown custom element: router-view - did you register the component correctly页面中不显示链接

    Vue报错:Unknown custom element: router-view - did you register the component correctly vue-router应用到组件 ...

  8. vue报错:Unknown custom element: <xxx> - did you register the component correctly?

    vue报错:Unknown custom element: - did you register the component correctly? For recursive components, ...

  9. 前端漫漫-Element UI报错:Unknown custom element: <el-menu>

    前端漫漫-Element UI报错:Unknown custom element: 问题描述:编写VUE文件,从Element UI官网复制组件信息的时候,引用了Element UI的html标签,但 ...

最新文章

  1. shell [] [[ ]] {}区别
  2. 前端开发中的Error以及异常捕获
  3. HTML4.0 / XHTML 1.0 Reference Manual
  4. ARPG游戏引擎设计思路
  5. git钩子放服务器_如何在GitLab中添加服务器端的预接收钩子?
  6. Ubuntu16.04下安装MySQL
  7. Android在全球的市场份额跃居全球第一
  8. Android 封装handler,android封装工作线程跟Handler工具类
  9. 使用Amazon s3托管您的Maven工件
  10. 1027. 打印沙漏(20)-PAT乙级真题
  11. [面试] 算法(二)—— 第一个出现一次的字符(第一个出现 k 次、出现最多次)
  12. numactl mysql_CentOS学习笔记 - 10. 开发机mysql安装
  13. Chome 浏览器,您的连接不是私密连接
  14. Matlab GUI编程技巧(四):m文件与simulink之间数据传递
  15. acwing 合唱队形
  16. ceph 代码分析 读_Ceph代码分析-OSD篇
  17. 异常你看这一篇就行了,全程白话很好理解(完结撒花)
  18. 饶有趣味的字、词、句——《语文常谈》读书笔记(4)
  19. 让数字人出圈的技术秘籍,华为率先公开了
  20. python中iadd与add_如何为Python属性实现-uyu iadd_uu

热门文章

  1. boost::filesystem模块Microsoft TCHAR 的使用示例的测试程序
  2. Boost:递归容器的测试程序
  3. Boost:bimap双图的突变的测试程序
  4. ITK:复制过滤器filter
  5. VTK:Filtering之ExtractVisibleCells
  6. OpenCV基本线性变换轨迹栏的实例(附完整代码)
  7. Julia OpenCV绑定简介
  8. OpenGL Tessellated Triangle镶嵌三角形的实例
  9. OpenGL 颜色Colors
  10. QT的QQmlApplicationEngine类的使用