2012-05-23 25 views
0
<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:使用ID或cssselector從下拉列表的webdriver - 從下拉列表中選擇國家,而不使用XPath

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

如何選擇英國?

謝謝!

回答

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?

+0

謝謝。將堅持xpath這個元素。我儘量避免在webdriver中使用xpath。 – SUM

相關問題