我想知道如何在ddl(下拉列表)中找到它後選擇元素。我的功能可以找到元素,但它沒有選擇它;我嘗試點擊,但沒有奏效。
感謝
的代碼是:使用java從下拉列表中選擇元素
public static void selectGuestCountry()
{
WebElement droplist = driver.findElement(By.className("sbOptions"));
List<WebElement> allOptions = droplist.findElements(By.tagName("a"));
System.out.println("Total options in list --> " + allOptions.size());
for (int i=0;i<allOptions.size();i++)
{
//System.out.println("++> " + allOptions.get(i).getAttribute("text"));
if(allOptions.get(i).getAttribute("text").equals("FRANCE")) {
System.out.println("++++>" + allOptions.get(i).getAttribute("text"));
**allOptions.get(i).click();**// This is doesn't select the country
break;
}
}
---------------------------------------------------------------
下面是HTML代碼,感謝:
<div class="guestForm">
<input id="reservations0.guests0.id" type="hidden" value="0" name="reservations[0].guests[0].id">
<br>
<div class="form-border">
<h5>
<div class="field-col left">
<div class="field-col right">
<div>
<div class="emails">
<div class="contactInfo">
<div class="field-row">
<label class="ellipsis" title="Organization">Organization</label>
<input id="reservations0.guests0.organization" class="field" type="text" maxlength="40" value="" name="reservations[0].guests[0].organization">
</div>
<div class="field-row select-big country">
<label>
<select id="reservations0.guests0.address.country.alpha2Code" class="selectbox" onchange="populateStates(this);" name="reservations[0].guests[0].address.country.alpha2Code" sb="73833889" style="display: none;">
<div id="sbHolder_73833889" class="sbHolder">
<a id="sbToggle_73833889" class="sbToggle" href="#"></a>
<a id="sbSelector_73833889" class="sbSelector" href="#">Select Country</a>
<ul id="sbOptions_73833889" class="sbOptions" style="display: none;">
<li>
<li>
<a href="#US" rel="US">UNITED STATES</a>
</li>
<li>
<a href="#AF" rel="AF">AFGHANISTAN</a>
</li>
<li>
<li>
<li>`
你能編輯你的問題並添加相關的html嗎? – LittlePanda
你可以試着明確地等待元素'allOptions.get(i)'嗎?等待元素可點擊 – LittlePanda
可能的重複[如何選擇selenium webdriver下拉值](http://stackoverflow.com/questions/20138761/how-to-select-a-dropdown-value-in-selenium -webdriver) –