如何搜索多个下拉列表中的下一个元素

时间:2018-03-06 19:09:00

标签: java selenium webdriver dropdown

两次选择相同的下拉元素。覆盖已经选定的船长。如何确保下一个元素选择Wicket守护者而不是之前选择的相同。除了名字,没有其他方法可以找到元素。

这是代码:

Select dropC = new Select(driver.findElement(By.name("playerRole")));
dropC.selectByVisibleText("Captain");

Select dropW = new Select(driver.findElement(By.name("playerRole")));
dropW.selectByVisibleText("Wicket Keeper");

这是html代码: select ng-model =“playerRole”name =“playerRole”id =“plr16”ng-change =“selectPlayersRole(this)”class =“ng-valid ng-dirty ng-valid-parse ng-touching”>球员队长Wicket Keeper队长/ Wicket守门员

有一个包含11个下拉列表的列表,我需要将其中一个下拉列表分配为“上尉”,另一个下拉列为“wicketkeeper”。

1 个答案:

答案 0 :(得分:0)

在一个数组中选择11名球员说 String []球员= {"队长"," Wicket Keeper"," Batsman",&#34 ; Batsman"," Batsman"," Batsman" " Batsman"," Bowler"," Bowler"," Bowler"," Bowler"} 然后写下面的代码来选择它们

List<WebElement> playerLocators = driver.findElements(By.name("playerRole"));
int playerCounter = 0;
for(WebElement playerLocator : playerLocators ) {
new Select(playerLocator).selectByVisibleText(players[playerCounter++]);
}

所有11名球员将分别被选中。

注意:如果定位器 By.name(&#34; playerRole&#34;)有超过11个下拉列表,代码可能会因为异常 ArrayIndexOutOfBoundsException