String[] expected = new String[10];
//{"Acapulco","Frankfurt","London","New York","Paris","Portland","San Francisco","Seattle","Sydney","Zurich"};
expected[0] = "Acapulco";
expected[1] = "Frankfurt";
expected[2] = "London";
expected[3] = "New York";
expected[4] = "Paris";
expected[5] = "Portland";
expected[6] = "San Francisco";
expected[7] = "Seattle";
expected[8] = "Sydney";
expected[9] = "Zurich";
List<WebElement> allOptions = driver.findElements(By.name("fromPort"));
// match the fromPort list value against the expected Array
for (int i = 0 ; i < (expected.length) && i < allOptions.size(); i++) {
String optionValue = allOptions.get(i).getAttribute("value");
if (optionValue.equals(expected[i])) {
System.out.println("PASSED on: " + optionValue + " we had: "+ expected[i] );
}
else {
System.out.println("FAILED on: " + optionValue + " we expected: " + expected[i]);
}
}
//close Firefox
driver.close();
当执行循环只进行一次然后它存在...所以它执行: 通过:阿卡普尔科我们有:阿卡普尔科 然后退出循环...
以下是我的其他评论:My Other Comment
所有这些Java和Selenium的新手......
我假设它的尺寸只有1;但不知道如何增加/克服这个
根据评论中的要求添加了HTML代码段:
<tr>
<td align="right">
<font face="Arial, Helvetica, sans-serif" size="2">
<b>
Departing
From:
</b>
</font>
</td>
<td>
<select name="fromPort">
<option value="Acapulco"></option>
<option value="Frankfurt"></option>
<option value="London"></option>
<option value="New York"></option>
<option value="Paris"></option>
<option value="Portland"></option>
<option value="San Francisco"></option>
<option value="Seattle"></option>
<option value="Sydney"></option>
<option value="Zurich"></option>
</select>
答案 0 :(得分:0)
请使用以下代码选择所有选项。您尝试使用的代码选择了select元素本身,而不是选项。
List<WebElement> allOptions = driver.findElement(By.name("fromPort")).findElements(By.tagName("option"));