webdriver - 从下拉列表中选择国家而不使用xpath

时间:2012-05-23 05:23:51

标签: selenium-webdriver

<a id="SIU2_country" class="button combobox-button country" onclick="event.cancelBubble = true; if (event.stopPropagation) { event.stopPropagation(); }" tabindex="9" style="cursor: auto;">
<span id="SIU2_country_span">&nbsp;</span>
<div class="combobox-menu" style="width: 298px; max-height: 212px; display: block;">
<div class="combobox-menu-item">
<span>United States</span>
</div>
<div class="combobox-menu-item">
<span>United Kingdom</span>
</div>
<div class="combobox-menu-item">

尽量避免在webdriver中使用xpath:

(By.xpath("//a[@id='SIU2_country']/div/div/span[contains(text(),'United Kingdom')]"));

如何使用id或cssselector从下拉列表中选择英国?

谢谢!

1 个答案:

答案 0 :(得分:0)

怎么样

List<WebElement> menuItemsList = driver.findElement(By.id("SIU2_country")).findElements(By.className("combobox-menu-item"));
WebElement menuItem = menuItemsList.get(1).findElement(By.tagName("span"));

虽然有点难看......请问这里使用xpath有什么问题?