【问题标题】:Selenium wait for ExpectedConditions.attributeToBe is not behaving as expectedSelenium 等待 ExpectedConditions.attributeToBe 的行为不符合预期
【发布时间】:2019-06-22 12:44:59
【问题描述】:

我正在尝试对元素属性执行 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;");

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

【问题讨论】:

标签: java eclipse selenium expected-condition


【解决方案1】:

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

wait.until(
ExpectedConditions.attributeToBe(By.id("my-element-id"), 
"atrributeNameString", 
"attributeValueString" )
);`

【讨论】:

    【解决方案2】:

    attributetobe会执行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;) 
    

    比较字符串

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      相关资源
      最近更新 更多