1

I am trying to do a wait.until on an element attribute as follows...

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;"));
        }
    }

I know from the print line that the attribute is as expected i.e. "top: 0px;" but when I step through the code on the wait.until the element in the UI is "clicked" and changes to closed (in this case the style changes to "top: 120px;"). Then the method starts at the beginning and then fails because it's now wrong.

Any help with why the method reruns and changes the value would be appreciated.

I've also tried...

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

But this fails for other reasons hence trying the alternative.

alex
  • 135
  • 3
  • 17

2 Answers2

1

atributetobe will execute equals string method, so if there is something else inside style, it will fail, try with attribute contains:

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

this will not work:

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

you need to use :

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

to compare strings

0

Locating the element By.id("my-element-id") has no issues.

wait.until(
ExpectedConditions.attributeToBe(By.id("my-element-id"), 
"atrributeNameString", 
"attributeValueString" )
);`
Kickaha
  • 3,680
  • 6
  • 38
  • 57