当我运行IE驱动程序时,它总是在下拉列表中选择错误的项。它似乎只发生在最后的项目在下拉框中。
例如,我想在下拉框中选择项目9,但是当我运行下面的代码时,它会选择item 8,这只发生在IE驱动程序中。
在运行此操作时,它选择了错误的项。
Dropdownbox.get(9).click();
当我运行它时,它会选择正确的项。
Dropdownbox.get(2).click();
我的环境: Selenium 3.0.0和IEWebdriver3.0.0.0,我也使用POM (页面对象模型)
@FindBy(how = How.CLASS_NAME,using = "select2-result-label")
private List<WebElement> Dropdownbox;
发布于 2016-11-21 14:26:02
经过进一步调查。我必须识别三个元素:下拉列表框中的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));
}
https://stackoverflow.com/questions/40719219
复制相似问题