選擇的選項在下拉框中我有類似這樣的下拉:獲取當前的硒
<select id="PlaceOfBirth">
<option value="">-- Please select --</option>
<option value="1">Somewhere</option>
<option value="2">Somewhere else</option>
<option value="3">somewhere else again</option>
</select>
在硒現在,我要確保,在頁面加載,-- Please select --
被默認選中。
谷歌搜索,selecting by xpath and [selected="selected"]看起來很有希望,但是,我不能使用它,因爲我的options
沒有選擇屬性開始。
因此,我認爲,相反,我可以選擇的第一要素,像這樣:
driver.FindElement(By.XPath("//select[@id='PlaceOfBirth']/*[1]"));
//the actual xpath being //select[@id='PlaceOfBirth']/*[1]
雖然這確實很好地返回第一個選項,它並不一定是選擇之一。我如何確保當前選定的選項是我想要的 (value="", text="-- Please select --")
?
謝謝。
PS:我使用的硒的webdriver
PPS:我所遇到的各種答案,像selectLocator
等,但他們要麼是PHP/Java或我無法找到他們在我的命名空間...
請參閱[本答案](http://stackoverflow.com/a/5296364)瞭解如何在WebDriver中創建一個'SelectElement'。一旦你有了,'SelectedOption'屬性將使你能夠驗證頁面加載時選擇的內容。 – Bryan 2011-12-28 18:44:44
@Bryan,非常感謝!這確實導致我幾乎沒有意識到的東西。 – LocustHorde 2011-12-29 15:20:22