John Collins
John Collins

Reputation: 89

How do I make Selenium webdriver to wait for an element to change its attribute to something else in Java

Below is my element;

String sId = driver.findElement(By.xpath(path)).getAttribute("data-id");

Since now the attribute value is stored in "sId", I now need to tell Selenium to wait until data-id attribute value is NOT equal to sID. I have tried the code below with no luck:

wait.until_not(ExpectedConditions.textToBePresentInElement(By.xpath(path), sId));

I get the error:

The method until_not(ExpectedCondition) is undefined for the type WebDriverWait

Also even if I change "until_not" to "until" I get this warning:

The method textToBePresentInElement(By, String) from the type ExpectedConditions is deprecated

How do I do it?

Upvotes: 1

Views: 1622

Answers (3)

Guy
Guy

Reputation: 50899

If you are checking the attribute value checking the text won't help you, those are two different things.

You can combine the not and attributeToBe ExpectedConditions for that

wait.until(ExpectedConditions.not(ExpectedConditions.attributeToBe(By.xpath(path), "data-id", sId)));

Upvotes: 2

frianH
frianH

Reputation: 7563

You can try bellow code

String sId = driver.findElement(By.xpath(path)).getAttribute("data-id");        

boolean expectedCondition = false;
boolean timeOut = false;
int second = 1;
do {
    try {
        //timeout for 60 seconds
        if(second>60) {
            timeOut = true;
        }
        String expectedId = driver.findElement(By.xpath(path)).getAttribute("data-id");
        if(! expectedId.equals(sId)) {
            expectedCondition=true;
        }else {
            second++;
        }
    } catch (Exception e) {
        TimeUnit.SECONDS.sleep(1);
        second++;
    }
}while(expectedCondition==false && timeOut==false);

Upvotes: 0

nitin chawda
nitin chawda

Reputation: 1388

You can try to use not from Expected conditions, for example:

wait.until(ExpectedConditions.not(ExpectedConditions.textToBePresentInElement(<element>, <elementtext>)));

Or you can also use expected conditions invisibility method:

wait.until(ExpectedConditions.invisibilityOfElementWithText(locator, text));

Upvotes: 1

Related Questions