忽略区分大小写,同时从Selenium网络驱动程序的下拉列表中选择选项



我使用忽略区分大小写从下拉菜单中选择时遇到问题selectByVisibleText。选项的情况是动态的。

我使用的示例代码:

public static void setDropdownValue(By fieldId, String fieldValue) {
    Select dropDown = new Select(driver.findElement(fieldId));
    dropDown.selectByVisibleText(fieldValue);
}

有没有办法从菜单中选择忽略案例的选项。

谢谢

使用selectByVisibleText(text)是不可能的,但如果你仍然想以某种方式做到这一点。使用类似这样的东西:

public static void setDropdownValue(By fieldId, String fieldValue) {
Select dropDown = new Select(driver.findElement(fieldId));
int index = 0;
    for (WebElement option : dropDown.getOptions()) {
        if (option.getText().equalsIgnoreCase(fieldValue))
            break;
        index++;
    }
    dropDown.selectByIndex(index);
}

最后要补充一点,根据您的网页结构,有时option.getText()可能不会返回您需要的选项值。

在这种情况下,请在下拉列表中找到包含选项值的属性并使用 option.getAttribute("the attribute name containing value") 。通常,对于"选择",属性是"值",为此必须使用option.getAttribute("value")

我希望它对:)有所帮助

,不是用selectByVisibleText(text),它变成了:

findElements(By.xpath(".//option[normalize-space(.) = " + Quotes.escape(text) + "]"));

参考: https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/support/ui/Select.java#L112

相关内容

最新更新