问题描述
我有一个问题,可以使用SelectByValue从下拉菜单中选择,而忽略大小写。
例如:日本阿尔巴尼亚
可以看到,日本是有价值的。 但是,我的值可以是“日本”或“日本”。
我可以选择使用。 但是,如果列表很大,则需要花费大量时间。
// 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;
}
}
我尝试使用适当的输入选择类,它比我的代码快得多。 有什么方法可以忽略selectByValue中的区分大小写。
感谢帮助
1楼
Selenium的selectByValue()实现使用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,因此我无法对其进行测试,但它应该非常接近。 让我知道这是否更快。