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

今天在调试脚本时,遇到如下报错:

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

按字面表达的意思大概是,所引用的元素已过时,不再依附于当前页面。通常情况下,这是因为页面进行了刷新或跳转,

下面说下我的情况:

我进入页面dom结构,查看发现原来table表格的id实时自增,即id=id++;这种模式,坑死了,加上度娘找的自己理解,终于解决了该问题解决方案如下:

  1. 因为是动态id,也就是说实时刷新,因此采取相对路径定位, xpath加and锁定定位条件,建议至少两个约束。
  2. 进到列表页时,先执行refresh方法,在获取到当前列集合,在去操作想要操作元素即可。

 

原文地址:https://www.cnblogs.com/longronglang/p/7300130.html