经过进一步调查。我必须确定 3 个元素:下拉框、下拉框中的 allOptions 和下拉框中的 inputTextbox。
我创建了这个方法来解决这个问题
public static void selectItemInDropdownBox(WebElement dropdownbox,WebElement inputSearch,List<WebElement> allOptionsList,String selectedItem){
//Wait for dropdownbox to display on page
browser.ExplicitWait(dropdownbox);
//Now Click on dropdownbox to show the inputTextbox and allOptions
dropdownbox.click();
// Must now wait for allOptions to display
browser.ExplicitWait(inputSearch);
// Type now the searched Item
inputSearch.sendKeys(selectedItem);
//Now if the search item has more than 1 returned item then we need to select the correct one
int counter = 0;
for ( WebElement i: allOptionsList) {
if ( i.getText().trim().equals( selectedItem ) ) {
allOptionsList.get(counter).click();
break;
}
counter++;
}
}
这是我的显式等待:第一个用于 WebElement,第二个用于列表
public static void ExplicitWait( WebElement WebElement){
(new WebDriverWait(driver,10)).until(ExpectedConditions.elementToBeClickable(WebElement));}
public static void ExplicitWaitList(List<WebElement> listWebElement){
(new WebDriverWait(driver,10)).until(ExpectedConditions.visibilityOfAllElements(listWebElement));
}