Selenium - Wait until element has a new value

1.5k views Asked by At

Say I have an element with value = 1. Then I click on some button, it takes a while to load and then the element value updates, say value = 2. Now, what I want to do - As soon as I clicked the button, I want to constantly check the element value, wait during the time period and as soon as the value updates (i.e. 2), the wait should stop.

Currently I use, simple Thread.sleep(milliseconds) to just explicitly wait for some time.

I was hoping to use the WebDriverWait with Expected Conditions in this scenario, but not sure how to do the constant check on the value and end the wait as soon as the value gets updated to 2.

Kindly suggest how can I achieve this?

1

There are 1 answers

0
Pap Nándor On

My solution in Java:

  1. By locator = By.xpath("YourXPath"); // You can use other: cssSelector, id, etc.
  2. WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeoutIntInSeconds)); // timeoutIntInSeconds is an integer value
  3. wait.until(ExpectedConditions.attributeToBe(locator, "value", String.valueOf(2)));