![](/img/trans.png)
[英]Selenium Webdriver JAVA - Unable to select option from dropdown menu (JAVA)
[英]Unable to Select From Dropdown By Index in Selenium and Java
我了解如何使用selectByIndex方法從下拉菜單中選擇元素。 但是,當我嘗試在以下HTML代碼段中執行selectByIndex( select.selectByIndex (index)
)時:
<select id="destinationAllocationId" name="destinationAllocationId">
<optgroup label="Anywhere Virtual Cards">
<option value="1555555555f0a19">NNN0 [*-2453]</option>
<option value="1555555555f0519">NNN1 [*-8354]</option>
<option value="155555555550419">NNN2 [*-5765]</option>
<option value="155555555520919">NNN3 [*-8875]</option>
</optgroup>
</select>
</div>
我無法維持所選的選項。
一開始它似乎可以工作(下拉菜單打開,正確的選項以藍色突出顯示)。 但是,一旦程序繼續執行(與下拉菜單無關),則dropdown會恢復為原始狀態(默認選項)。
換句話說,程序最初會選擇適當的選項,但是它會自動恢復為默認選項,並且在下拉菜單消失后實際上不會更改下拉菜單中的值(通常,當我手動選擇一個選項時,下拉菜單關閉后即會選擇該選項,但是在這種情況下,只要下拉菜單處於打開狀態,就會選擇該選項,但關閉后會立即選擇默認選項)
任何想法為什么會這樣
我也有這個問題
這就是我解決的方法
/**
* helper method for handling drop downs
* @param idPrefix the id of the drop down without any _xy
* @param value the value to be selected
*/
public void clickDropdown(String idPrefix, String value) {
// find & click on the label element of the drop down
firefoxDriver.findElement(By.id(idPrefix + "_label")).click();
// find & choose element from drop down
firefoxDriver.findElement(By.xpath("//div[@id='" + idPrefix + "_panel']/div/ul/li[text()='" + value + "']")).click();
}
你這樣稱呼它:
// choosing an element in the drop down menu
clickDropdown("projectForm:stellvertreter", "StringToSelect");
我寫的是與primefaces一起使用的,您無論如何都必須對其進行修改,但是您知道
有關選擇和還原的問題...嘗試將Enter鍵發送到所選項目或再次單擊它...我的方法沒有
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.