Select 使用 Selenium Java 的动态非 select 下拉列表中的文本值

Select value by text from a dynamic non select dropdown using Selenium Java

我想 select 来自动态非 select 下拉列表的文本值。我做了一些研究,发现了这段代码:

WebElement element = driver.findElement(ByMapper.getBy(dropdown));
element.click();
List<WebElement> options = element.findElements(By.xpath("/html/body/div[1]/div[2]/div/div/div"));
for (WebElement option : options){
    if (option.getText().contains(text)){
        option.click();
        break;
    }
}

基本上它将下拉选项放入列表元素中,然后 运行 在 for 循环中通过,如果选项包含文本,则单击它并中断循环。但是它不适用于这种类型的下拉菜单:

快照:

你能建议我该怎么做吗?

特定值的快照:

注意:该页面只能通过私人 vpn 访问,因此我无法共享它。

到 select 文本值 Teszt_5 从动态非 Select 下拉列表您可以使用以下 :

String text = "Teszt_5";
WebElement element = driver.findElement(ByMapper.getBy(dropdown));
element.click();
List<WebElement> options = element.findElements(By.xpath("//div[@class='mat-autocomplete-panel mat-autocomplete-visible ng-star-inserted' and starts-with(@aria-labelledby, 'mat-form-field-label')]//mat-option//span[@class='mat-option-text']"));
for (WebElement option : options){
    if (option.getText().contains(text)){
        option.click();
        break;
    }
}

参考资料

您可以在以下位置找到一些相关的详细讨论: