Keeper
Keeper

Reputation: 61

Selenium: Getting the Value of DropDown after Waiting for the DropDown to Update

I have 2 select drop downs. When I select an option for the first drop down, it will automatically update the second one. I need to check if the value of the second drop down is the same as the first drop down.

My problem is that there are pre-loaded values in the second dropdown. If I access it, the return values are all those pre-loaded ones. I need to wait until the drop down value updates, then I will get the value for checking.

I tried to wait hoping that after some time the value will be updated but it results in an error. This is my code for waiting:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

I also can't use this because the element exists since the start.

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".z-phenotype-dropdown.z-select")));

The html code for the second dropdown goes like this:

<td>
<tbody>
<tr>
<select class="z-phenotype-dropdown z-select">
    <option class="z-option"> </option>
    <option class="z-option"> sample 1 </option>
    <option class="z-option"> sample 2 </option>
    <option class="z-option"> sample 3 </option>
</select>
</tr>
</tbody>
</td>

Upvotes: 2

Views: 1476

Answers (2)

Remya Nambala
Remya Nambala

Reputation: 1

Below may work as well:

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until (ExpectedConditions.presenceOfElementLocated (By.xpath ("//option[contains(text(),'sample 1')]")));

Upvotes: 0

Murthi
Murthi

Reputation: 5347

I assume the default number of elements in the drop down is 4 and waiting for the number of options is greater than 4 as given below. It may works for you.

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.cssSelector(".z-phenotype-dropdown.z-select>option"),4));

Upvotes: 2

Related Questions