1

我正在尝试对元素属性执行 wait.until ,如下所示...

public static void WaitForElementSize(WebElement element, WebDriver driver, int timeoutInSeconds)
    {
        if (timeoutInSeconds > 0)
        {
            System.out.print(element.getAttribute("style"));

            WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
            wait.until(ExpectedConditions.attributeToBe(element, "style", "top: 0px;"));
        }
    }

我从打印行知道该属性与预期的一样,即“top:0px;” 但是当我单步执行wait.until中的代码时,UI中的元素被“单击”并更改为关闭(在这种情况下,样式更改为“top:120px;”)。然后该方法从头开始,然后失败,因为它现在是错误的。

任何有关为什么该方法重新运行和更改值的帮助将不胜感激。

我也试过...

wait.until(e -> element.getAttribute("style") == "top: 0px;");

但这因其他原因而失败,因此尝试了替代方案。

4

2 回答 2

1

atributetobe 会执行 equals 字符串方法,所以如果 style 里面有别的东西,它会失败,尝试使用属性 contains:

wait.until(ExpectedConditions.attributeContains(element, "style", "top: 0px;"));

这不起作用:

wait.until(e -> element.getAttribute("style") == "top: 0px;"); 

你需要使用:

element.getAttribute("style").equals("top: 0px;) 

比较字符串

于 2019-01-29T12:23:43.363 回答
0

定位元素By.id("my-element-id")没有问题。

wait.until(
ExpectedConditions.attributeToBe(By.id("my-element-id"), 
"atrributeNameString", 
"attributeValueString" )
);`
于 2020-07-28T13:36:25.863 回答