- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个场景,我试图循环遍历条形图上的多个元素,直到找到“rect”标签名称。当我单击“矩形”标签名称时,将从图表中选择单个栏,并且我将重定向到另一个页面。请参阅下面我正在使用的条形图的图像: /image/U4LNm.jpg
作为引用,我正在使用的条形图位于右上角。我要执行的测试是单击图表中的第一个条;这样做会将我重定向到适当的页面。为此,我使用 Eclipse (Java) 在 Selenium Webdriver 中编写了以下代码:
WebElement deliveredChartDailyFocus = driver.findElement(By.id("delivered-chart-daily"));
deliveredChartDailyFocus.click();
List<WebElement> children = deliveredChartDailyFocus.findElements(By.tagName("rect"));
Iterator<WebElement> iter = children.iterator();
while (iter.hasNext()) {
WebElement we = iter.next();
if(we.isDisplayed()){
we.click();
}
一切似乎都运行良好,因为上面的代码点击了“rect”元素并将我重定向到适当的页面。然而,当我点击该页面时,我收到一个错误,因为代码仍在寻找不在新页面上的“矩形”值。
您会注意到上面缺少一个“break”行……这是因为,在调试代码时,我发现在迭代循环时,单击事件直到第三次迭代才会启动,我假设是因为“rect”元素不可见?因此,如果我放入“break”语句,它会在第一次迭代后退出循环,因此我永远不会到达执行“click”事件以导航到新页面的部分。
本质上,我所追求的是一种能够根据需要循环多次直到找到适当的“矩形”元素的方法。单击该按钮后,我将被重定向到新页面……只有在那时我才希望退出循环,以便不显示“NoSuchElementException 错误。
如果需要更多详细信息,请告诉我,非常感谢对此的任何指导。
最佳答案
一旦进入新页面,所有这些 rect
元素都会消失。对这些 rect
元素进行任何引用都将触发此 StaleElementReferenceException
。
所以不要在点击后引用这些元素。迭代到第一个显示的 rect 元素,然后停止迭代。
WebElement deliveredChartDailyFocus = driver.findElement(By.id("delivered-chart-daily"));
deliveredChartDailyFocus.click();
// Get a list of all the <rect> elements under the #delivered-chart-daily element
List<WebElement> children = deliveredChartDailyFocus.findElements(By.tagName("rect"));
WebElement elementToClick = null; // variable for the element we want to click on
for (WebElement we : children) // loop through all our <rect> elements
{
if (we.isDisplayed())
{
elementToClick = we; // save the <rect> element to our variable
break; // stop iterating
}
}
if (elementToClick != null) // check we have a visible <rect> element
{
elementToClick.click();
}
else
{
// Handle case if no displayed rect elements were found
}
关于java - 如何处理 StaleElementReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28193303/
我刚刚开始使用带有 webdriver 的 geb 进行自动化测试。 As I understand it ,当我在页面上定义内容时,每次调用内容定义时都应该查找页面元素。 //In the cont
我有一个非常简单的脚本,可以找到页面中的所有 HREF 并迭代并记录它: Photo search scroll Go To https://xxx ${elements}=
我正在为鼠标悬停工作,我想通过使用 for 循环单击每个链接来测试所有链接的工作状况。在我的程序中,迭代进行一次,而对于下一次迭代,它不起作用并显示“StaleElementReferenceExce
在我的网页上,我有一个部分链接列表,每个部分都有详细信息的链接。我尝试转到每个部分,然后验证所有链接都没有损坏。 List sections = driver.findElements(By.xpat
我有一个场景,我试图循环遍历条形图上的多个元素,直到找到“rect”标签名称。当我单击“矩形”标签名称时,将从图表中选择单个栏,并且我将重定向到另一个页面。请参阅下面我正在使用的条形图的图像: /im
我已经研究这个错误一段时间了,并尝试了很多方法,但似乎没有任何效果...... while(!driver.findElements(By.className("next")).isE
您好,我是 Selenium 的新手 我正在使用 Java 库,已经尝试了 Chrome 和 Firefox 驱动程序。 我正在运行一个循环。有趣的是,循环有时工作 3、2 次,它并不总是在同一次迭代
我正在测试新建的框架,在 Chrome 浏览器中工作时经常遇到 org.openqa.selenium.StaleElementReferenceException。框架设计会不会有问题?当我在其他浏
显示堆栈溢出错误 public static void main(String[] args) throws Exception { System.setProperty("webdriver
我在这样的测试中有wait: WebDriverWait wait = new WebDriverWait(dr, TimeSpan.FromSeconds(30)); ... wait.Until(
好吧,伙计们...我知道这个问题之前似乎已经得到解答,但我尝试了几种解决方案,但没有一个奏效。如果我再看到一次 StaleElementReferenceException,我就会丢掉它。我妻子的男朋
导航到不同页面并返回后,我收到 StaleElementReferenceException。我尝试过显式等待,再次重写定位器,但似乎不起作用。任何帮助将不胜感激。 Select select
我尝试用selenium 制作一个网络爬虫。我的程序引发 StaleElementReferenceException。我认为这是因为我递归地抓取页面,并且当页面没有更多链接时,该函数导航到下一页,而
我需要获取快速变化元素的值(在本例中是一个以毫秒为单位倒计时的计时器),但是当我尝试使用时 String timeLeftString = driver.findElement(By.xpath(ti
我有一个包含一堆表格的页面。我在外循环中遍历表,然后在内循环中遍历表中的每一行。一切正常。但是有些页面有下一步按钮。当我在完成页面后添加代码以单击它时,我开始在循环遍历表格的行时收到 StaleEle
在我的 Selenium 测试代码中有几行 点击一个复选框 从选择框中选择一个项目 点击按钮提交表单 这是代码 WebElement selectAllElement = driver.findEle
我在 Ubunto 上运行 Web E2E 测试(使用 cucumber、junit、selenium webDriver)。 使用remoteWebDriver时,我的测试偶尔失败(使用local-
我一直在使用 protractor/jasmine2 解决陈旧元素引用异常的问题。 我的规范: var LoginPage = require('../pages/login_page.js'); v
这个问题已经有答案了: Stale element exception python (2 个回答) Selenium clicks one time, but next click returns
运行测试时出现此错误:org.openqa.selenium.StaleElementReferenceException: 元素不再附加到 DOM 关于如何解决上述异常的任何想法?这发生在我的网格中
我是一名优秀的程序员,十分优秀!