下拉列表(组合框)中没有如何检查特定值。在我的应用程序中,当我创建一个项目时,该项目将添加到其中一个下拉列表中。当我删除该项目时,下拉列表中将无法使用该项目进行选择。那么如何检查该项目是否无法供用户在selenium中选择?
编辑:添加了它的图片。这是一个AngularJS DropDown。
答案 0 :(得分:1)
在假设使用简单的select方法
实现下拉列表的情况下粘贴代码String valueBeingChecked="xxxx";
int flag=0;
WebElement drpdwn=driver.findElement(by.id("DROPDOWN_ID");
Select DrpDwnSel=new Select(drpdwn);
List<WebElement> DrpDwnList=DrpDwnSel.getOptions();
For(WebElement indElem:DrpDwnList){
if (indElem.getText().contains(valueBeingChecked)){
Flag=1;
break;
}
}
Flag = 0,valueBeingChecked未列出
Flag = 1,valueBeingChecked列出
答案 1 :(得分:0)
那是我的美分。我用try catch。当找不到该选项时,它将更新标志。它比遍历列表更快。
public boolean isOptionExisted(String option) {
boolean result=true;
try {
Select selection= new Select(dropdownWebElement);
selection.selectByVisibleText(option);
}catch(Exception e) {
result = false;
}
return result;
}