java selenium webdriver Select 来自下拉菜单 select 按值忽略大小写

java selenium webdriver Select from dropdown menu select by value ignore case sensitivity

我在使用忽略大小写的 SelectByValue 的下拉菜单中 select 时遇到问题。

例如: 日本 阿尔巴尼亚

可见价值是日本。但是,我的值可以是 "japan" 或 "Japan"。

我可以 select 通过使用。但是,如果列表很大,则需要花费大量时间。

// get dropdown elements
Select dropdown = new Select(findElementHelper(by));
// get elements based on options from dropdown menu
List<WebElement> myElements = dropdown.getOptions(); //because of  listing takes time
// test until value of element and given value is equal
String tempValue = value.trim();
for (WebElement option : myElements) {
    if (tempValue.equalsIgnoreCase(option.getAttribute("value").trim())) {
        // tryClick(option,value) did not work on ie
        /*if (!tryClick(option,value)){
            System.out.println(value + " is not selected");
           return false;
       } option.click(); //worked one
        break;
    }
}

我已经尝试 Select class 使用正确的输入,它的运行速度比我的代码快得多。有什么办法可以在 selectByValue.

中忽略区分大小写

感谢帮助

selectByValue() 的 Selenium 实现使用 xpath 搜索给定值,而不是循环遍历所有选项。您应该能够将 xpath 搜索更改为将所有内容更改为小写。如果您知道只有一个选项具有给定值,那么您可以通过删除 List 和 for 循环来进一步简化它。

WebElement dropdownElement = findElementHelper(by);

String tempValue = value.trim().toLowerCase();

List<WebElement> matchingValues = dropdownElement.findElements(By.xpath(
        ".//option[lower-case(@value) = '" + tempValue + "']"));    

for(WebElement matchingValue : matchingValues)
{
     /* Do what you want with the options         
     if (!option.isSelected()) {
         option.click();
     } */
}

我在 Java 中没有使用 Selenium,所以我无法对此进行测试,但它应该非常接近。让我知道这是否更快。