首页 > 解决方案 > 从 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"

标签: javaseleniumdropdown

解决方案


请尝试使用以下代码:如果可能,请分享应用程序 URL,然后我可以从我身边复制它。

WebElement option = driver.findElement(By.xpath("//mat- 
      option[contains(text(),'option text')]");
driver.waitUntilElementVisible(option, 10); 
driver.findElement(option).click();

另请参阅链接以获取有关在不使用 Selenium 中的 Select 类方法的情况下在下拉列表中选择特定值的更多详细信息


推荐阅读