WebDriverWait用于更改元素属性

so *_*ead 19 selenium webdriver selenium-webdriver

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

在我的AUT中,我必须等待按钮在继续之前启用,不幸的是由于开发人员编写页面的方式我不能使用WebElement的isEnabled()方法.开发人员正在使用一些CSS来使按钮看起来像被禁用,因此用户无法点击它,方法isEnabled总是为我返回true.所以我要做的是获取属性"aria-disabled"并检查文本是"true"还是"false".到目前为止我一直在做的是使用Thread.sleep的for循环,如下所示:

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);
 }
Run Code Online (Sandbox Code Playgroud)

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

我确信有一种方法可以使用WebDriverWait实现类似的东西,这是我无法弄清楚如何的首选方法.这就是我想要实现的,即使以下方法不起作用:

WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOf(refresh.getText() == "true")); 
Run Code Online (Sandbox Code Playgroud)

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

Sri*_*dha 33

以下内容可能有助于您的要求.在下面的代码中,我们覆盖了包含我们正在寻找的条件的apply方法.因此,只要条件不为真,在我们的情况下,启用不是真的,我们进入循环最多10秒,每500毫秒轮询一次(这是默认值),直到apply方法返回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;
    }
});
Run Code Online (Sandbox Code Playgroud)