如何处理StaleElementReferenceException [英] How to handle StaleElementReferenceException

查看:196
本文介绍了如何处理StaleElementReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,我试图循环通过条形图中的一些元素,直到找到rect标签名称。当我点击rect标签名称时,从图表中选择单个栏,然后重定向到另一个页面。请参阅下面的图表,我正在处理:
http://imgur.com/xU63X1Z



为了参考,我正在使用的条形图是右上方。我想执行的测试是点击图表中的第一个栏;这样做,将重定向到适当的页面。为了做到这一点,我在Selenium Webdriver中使用Eclipse(Java)编写了以下代码:

  WebElement deliveredChartDailyFocus = driver.findElement (By.id( 递送-图表每日)); 
deliveredChartDailyFocus.click();

列表< WebElement> children = deliveredChartDailyFocus.findElements(By.tagName(rect));
迭代器< WebElement> iter = children.iterator();

while(iter.hasNext()){
WebElement we = iter.next();

if(we.isDisplayed()){
we.click();
}

一切似乎都很好,上面的代码点击了rect元素并将我重定向到相应的页面。但是,当我点击页面时,我会收到错误,因为代码仍在寻找不在新页面上的rect值。



我会注意到上面有一个break行缺失...这是因为在调试代码之后,我发现当循环遍历循环时,点击事件直到第3次迭代才会进行,我假设因为rect元素是不可见的?因此,如果我放入一个break语句,它会在第一次迭代后退出循环,因此我从来没有找到执行点击事件来导航到新页面的部分。



本质上,我之后是一种能够根据需要循环多次的方式,直到找到相应的rect元素。点击后,我被重定向到新页面...。那时我想要循环退出,所以NoSuchElementException错误不显示。



如果需要更多的细节,请告诉我们,真的很感谢任何有关这方面的指导。

解决方案

新页面所有这些 rect 元素都消失了。执行任何对 rect 元素的引用将触发此 StaleElementReferenceException



所以请点击后不要引用这些元素。迭代到第一个显示 rect 元素,然后停止迭代。

  WebElement deliveredChartDailyFocus = driver.findElement(By.id(deliver-chart-daily)); 
deliveredChartDailyFocus.click();

//获取所有< rect> #deliver-chart-daily元素下的元素
列表< WebElement> children = deliveredChartDailyFocus.findElements(By.tagName(rect));

WebElement elementToClick = null; //我们要单击
的元素的变量(WebElement我们:children)//循环遍历我们所有的< rect>元素
{
if(we.isDisplayed())
{
elementToClick = we; //保存< rect>元素到我们的变量
break; //停止迭代
}
}

if(elementToClick!= null)//检查我们有一个可见的< rect>元素
{
elementToClick.click();
}
else
{
//如果没有找到显示的rect元素,则处理案例
}


I have a scenario where I am attempting to loop through a number of elements on a bar chart, until I find the "rect" tag name. When I click on the "rect" tag name, the individual bar is selected from the chart, and I am redirect to another page. Please see below for an image of the bar chart I am working with: http://imgur.com/xU63X1Z

For reference, the bar chart I am working with is the top right-hand side. The test I want to execute is to click on the first bar in the chart; doing so, will redirect me to an appropriate page. In order to do this I have written the following code in Selenium Webdriver using Eclipse (Java):

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

Everything appears to work well in that the above code hits the "rect" element and redirects me to the appropriate page. However, when I hit the page I then get an error as the code is still looking for the "rect" value which isn’t on the new page.

You’ll notice that there is a "break" line missing from the above…..this is because, upon debugging the code I found that when iterating through the loop, the click event does not kick in until the 3rd iteration, I’m assuming because the "rect" element is not visible? Therefore, if I put in a "break" statement it exits out of the loop after the first iteration, and therefore I never get to the part where I carry out the "click" event to navigate to the new page.

Essentially, what I’m after is a way of being able to loop as many times as necessary until the appropriate "rect" element can be located. Upon clicking on that, I am redirected to the new page….only at that point do I want the loop to exit, so that the "NoSuchElementException error is not displayed.

If any more details are required please let me know, would really appreciate any guidance on this.

解决方案

Once you're on the new page all those rect elements are gone. Exercising any references to those rect elements will trigger this StaleElementReferenceException.

So don't reference those elements after the click. Iterate to the first displayed rect element then stop iterating.

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
}

这篇关于如何处理StaleElementReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆