等待Element过时,为什么'ExpectedConditions.stalenessOf'不起作用? [英] Waiting for Element to become stale, why doesn't 'ExpectedConditions.stalenessOf' work?

查看:289
本文介绍了等待Element过时,为什么'ExpectedConditions.stalenessOf'不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了以下方法:

public void waitAndClickElement(WebElement element) throws InterruptedException {
    try {
        Boolean elementPresent = this.wait.until(ExpectedConditions.elementToBeClickable(element)).isEnabled();
        if (elementPresent == true && element.isDisplayed()) {
            element.click();
            System.out.println("Clicked on the element: " + element.getText());
        }
    } catch (StaleElementReferenceException elementUpdated) {
        Boolean elementPresent = wait.until(ExpectedConditions.stalenessOf(element));
        if (elementPresent == true) {
            WebElement staleElement = element;
            staleElement.click();
            System.out.println("Clicked on the 'Stale' element: " + element.getText());
        }
    } catch (NoSuchElementException e) {
        System.out.println("Exception! - Could not click on the element: " + element.getText() + ", Exception: "+ e.toString());
        throw (e);
    } finally {
    }
}

但是我似乎仍然在下面得到以下异常:

But I Still seem to get the following exception below:

预期条件失败:等待元素(代理元素:DefaultElementLocator'By.xpath://a [text()='Exchange Now»']')过时(尝试使用500进行20秒MILLISECONDS间隔)

Expected condition failed: waiting for element (Proxy element for: DefaultElementLocator 'By.xpath: //a[text()='Exchange Now »']') to become stale (tried for 20 second(s) with 500 MILLISECONDS interval)

但是同样的方法可以用在20个构建中的18个,有什么想法吗?

But the same method will work on let's say 18 out of 20 builds, any ideas?

感谢您的帮助

推荐答案

问题的直接答案是,您正在等待已经过时的元素变得过时.我不确定您的意图是什么.

The direct answer to your question is that you are waiting for an already stale element to become stale. I'm not sure what your intent was there.

您的功能过于复杂,无法正常运行.

Your function is overly complex and won't work as you think it might.

如果某个元素是可单击的,则也会启用并显示该元素,因此您无需检查所有三个元素.如果元素抛出StaleElementReferenceException,则不会变得过时".

If an element is clickable it's also enabled and displayed, so you don't need to check all three. If the element is throwing a StaleElementReferenceException, it's not going to become "unstale."

我建议您用以下内容替换当前功能.

I would recommend you replace your current function with the below.

public void waitAndClickElement(By locator)
{
    try
    {
        this.wait.until(ExpectedConditions.elementToBeClickable(locator)).click();
    }
    catch (TimeoutException e)
    {
        System.out.println("Count not click element <" + locator.toString() + ">");
    }
}

传递定位器而不是元素,而不是元素本身,这将简化很多事情.如果该元素存在并且可单击,则将其单击.如果它不存在或永远不可单击,它将抛出一个TimeoutException,您可以抓住它.

Pass the locator instead of the element instead of the element itself, that will simplify a lot of things. If the element exists and is clickable, it will be clicked. If it does not exist or never becomes clickable, it will throw a TimeoutException which you can catch.

此外,编写element.toString()还将为该元素写一些ID,该ID将不会被人类阅读或有意义.您最好编写locator.toString(),它将返回定位器的类型,例如By.cssSelector: #hplogo.

Also, writing element.toString() is going to write some ID for the element which is not going to be human readable or meaningful. You're better off to write locator.toString() which will return the type of locator, e.g. By.cssSelector: #hplogo.

这篇关于等待Element过时,为什么'ExpectedConditions.stalenessOf'不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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