WebDriverWait更改元素属性 [英] WebDriverWait for an element attribute to change

查看:231
本文介绍了WebDriverWait更改元素属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用WebDriverWait等待属性更改?

How can I use WebDriverWait to wait until an attribute changes?

在我的AUT中,我必须等待一个按钮启用后才能继续,但是不幸的是,由于开发人员对页面进行编码的方式,我无法使用WebElement的isEnabled()方法.开发人员正在使用一些CSS来使按钮看起来像已禁用,因此用户无法单击它,并且isEnabled方法始终为我返回true.因此,我要做的就是获取属性"aria-disabled",并检查文本是"true"还是"false".到目前为止,我一直在做Thread.sleep的for循环,就像这样:

In my AUT I have to wait for a button to become enabled before continuing, unfortunately due to the way the developer coded the page I can't use WebElement's isEnabled() method. The developer is using some CSS to just make the button look like it's disabled so the user can't click on it and the method isEnabled always returns true for me. So what I have to do is get the attribute "aria-disabled" and check whether the text is "true" or "false". What I've been doing so far is a for loop with Thread.sleep, something like this:

for(int i=0; i<6; ++i){
    WebElement button = driver.findElement(By.xpath("xpath"));
    String enabled = button.getText()
    if(enabled.equals("true")){ break; }
    Thread.sleep(10000);
 }

(如果不正确,请忽略上面的代码,只是我正在做的伪代码)

(disregard the code above if incorrect, just pseudo code of what I'm doing)

我敢肯定有一种方法可以使用WebDriverWait实现类似的效果,这是我不知道怎么做的首选方法.这是我试图实现的目标,即使以下情况行不通:

I'm sure there's a way to achieve something similar using WebDriverWait which is the preferred method I just can't figure out how. This is what I'm trying to achieve even though the following won't work:

WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOf(refresh.getText() == "true")); 

显然,它不起作用,因为该函数期望的是WebElement而不是String,但这是我要评估的东西.有任何想法吗?

Obviously it doesn't work because the function is expecting a WebElement not String but it's what I'm trying to evaluate. Any ideas?

推荐答案

以下内容可能会帮助您解决问题. 在下面的代码中,我们将覆盖包含我们要查找的条件的apply方法.因此,只要条件不成立(在我们的情况下,启用条件不成立),我们就会循环最多10秒,每500毫秒轮询一次(这是默认设置),直到apply方法返回true为止.

The following might help your requirement. In the following code, we override apply method incorporating the condition that we are looking for. So, as long as the condition is not true, in our case, the enabled is not true, we go in a loop for a maximum of 10 seconds, polling every 500 ms (this is the default) until the apply method returns true.

WebDriverWait wait = new WebDriverWait(driver,10);

wait.until(new ExpectedCondition<Boolean>() {
    public Boolean apply(WebDriver driver) {
        WebElement button = driver.findElement(By.xpath("xpath"));
        String enabled = button.getAttribute("aria-disabled");
        if(enabled.equals("true")) 
            return true;
        else
            return false;
    }
});

这篇关于WebDriverWait更改元素属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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