【问题标题】:Selenium: Getting the Value of DropDown after Waiting for the DropDown to UpdateSelenium:等待 DropDown 更新后获取 DropDown 的值
【发布时间】:2017-06-29 01:25:50
【问题描述】:

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

我的问题是第二个下拉菜单中有预加载的值。如果我访问它,返回值都是那些预加载的。我需要等到下拉值更新,然后我会得到检查的值。

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

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>

【问题讨论】:

  • 你能给我们同样的HTML代码吗?

标签: java selenium wait dropdown


【解决方案1】:

我假设下拉列表中的默认元素数量为 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.numberOfElementsToBeLessThan(By.cssSelector(".z-phenotype-dropdown.z-select>option"), 3));
【解决方案2】:

以下也可以:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 2020-10-12
    • 2019-01-02
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 2023-03-07
    相关资源
    最近更新 更多