java - 从 div 类下拉列表中选择 - Selenium 但它不起作用
问题描述
我正在尝试从下拉列表中选择一个选项,该选项在单击定位器之前不会填充。这是我的解决方案,但它不起作用。
List<WebElement> options = driver.findElements(By.cssSelector("mat-select"));
for (WebElement option : options) {
if (option.getAttribute("ng-reflect-value").contentEquals("50757")) {
Actions build = new Actions(driver);
build.moveToElement(option).click().build().perform();
}
}
下拉列表的 HTML。
<div role="listbox"
tabindex="-1"
class="ng-tns-c114-22 ng-trigger ng-trigger-transformPanel mat-select-panel mat-primary"
id="mat-select-0-panel"
aria-multiselectable="false"
aria-labelledby="mat-form-field-label-27"
style="transform-origin: 50% 22px 0px; font-size: 15px; opacity: 1; min-width: calc(100% + 32px); transform: scaleY(1);">
<mat-option
_ngcontent-tqo-c274=""
role="option"
class="mat-option mat-focus-indicator mat-active ng-tns-c114-22 ng-star-inserted"
ng-reflect-value="50757"
id="mat-option-0"
tabindex="0"
解决方案
请尝试使用以下代码:如果可能,请分享应用程序 URL,然后我可以从我身边复制它。
WebElement option = driver.findElement(By.xpath("//mat-
option[contains(text(),'option text')]");
driver.waitUntilElementVisible(option, 10);
driver.findElement(option).click();
另请参阅链接以获取有关在不使用 Selenium 中的 Select 类方法的情况下在下拉列表中选择特定值的更多详细信息
推荐阅读
- identityserver4 - 静态文件 - asp.net web api core 2.1 身份验证方案 Bearer
- vue.js - 当我们从 vuejs 中的选择中选择某个选项时如何显示或隐藏表单元素
- android - mvc中的Web服务从android获取多部分数据
- python - 即使使用相同的解释器从 shell 运行,使用 python IntelliJ 运行也不起作用
- php - 在 eloquent 中选择加入 laravel
- jquery - Jquery如何找出我是否有一个选中的复选框然后将其放入if语句中
- android - GET_ACCOUNT 权限未在 android N 中授予
- java - Spock Maven - 无法使用现有的 Java 单元测试代码和 *.groovy 文件运行
- laravel - 在添加之前对数据库中的添加值进行哈希处理
- google-api - 通过 YouTube 数据 API 验证 YouTube Premium 会员