Selenium:等待DropDown更新后获取DropDown的值

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

我有 2 个 select 下拉菜单。当我 select 第一个选项下拉时,它会自动更新第二个。我需要检查第二个下拉菜单的值是否与第一个下拉菜单相同。

我的问题是第二个下拉列表中有预加载的值。如果我访问它,return 值都是那些预加载的值。我需要等到下拉值更新,然后我将获取值进行检查。

我试图等待,希望一段时间后该值会更新,但它会导致错误。这是我等待的代码:

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

我也不能使用它,因为该元素从一开始就存在。

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

第二个下拉菜单的 html 代码如下:

<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>

我假设下拉列表中的默认元素数是 4,等待选项数大于 4,如下所示。它可能适合你。

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

以下可能也有效:

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