java - 如何单击下拉菜单并通过 Selenium 和 Java 选择一个选项?
问题描述
我正在尝试单击下拉按钮,代码运行成功,但未在 selenium 中单击下拉按钮。
<select id="comp_drpdwn" data-select-id="f41d68e6-38b3-19cc-c392-3c9fc33690e8" class="initialized">
<option value="0">Select Company</option>
<option value="Acme~54sc234xb">Acme</option>
<option value="Acme Bar & Grill~TI17267302">Acme Bar & Grill</option>
<option value="Ball & Chain~TI58377308">Ball & Chain</option>
我的代码是:
WebElement element = driver.findElement(By.xpath(".//*[@id='comp_drpdwn']"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
解决方案
这应该有效:
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='comp_drpdwn']")));
Select select = new Select(element);
select.selectByVisibleText("Acme");
推荐阅读
- sql - SQL regular expression for date
- asynchronous - Async.Start 与 timeout 和 cancelToken?
- azure-active-directory - 在 Graph API beta 端点上创建 Azure AD 应用程序时出错
- php - Yajra 数据表同时使用搜索和自定义过滤器
- php - 如何通过 Php 解密 SHA512 密码?
- vb.net - 使用VB.Net在文本文件中加载列表框项目
- java - 声纳 - 将 DATE_FORMAT 作为实例变量
- c# - `Adapter.Update(DataTable);`方法更新表数据源的所有行还是只更新那些已经修改的行?
- algorithm - 加权无向图中的最长路径
- javascript - 解析 csv 文本以创建引导 div?