我有一個網頁下拉選擇一個國家,已使用jQuery選擇插件呈現。下面的HTML的提取物,CssSelector Webdriver FindElement點擊不工作
<div>
<label for="phMainContent_EmployeeAdd1_ddlCountry" id="phMainContent_EmployeeAdd1_lblCountry" class="short required">Country*</label>:
<div id="phMainContent_EmployeeAdd1_ddlCountry_chzn" class="chzn-container undefined chzn-container-single" style="width: 199.44444px;">
<a href="#x" class="chzn-single"><span>Please select ...</span><div><b></b></div></a>
<div class="chzn-drop" style="left: -9000px; width: 197.222px; top: 28px;">
<div class="chzn-search"><input type="text" style="width: 162px;"></div>
<ul class="chzn-results">
<li id="phMainContent_EmployeeAdd1_ddlCountry_chzn_o_0" class="active-result result-selected">Please select ...</li>
<li id="phMainContent_EmployeeAdd1_ddlCountry_chzn_o_1" class="active-result">United Kingdom</li>
<li id="phMainContent_EmployeeAdd1_ddlCountry_chzn_o_2" class="active-result">Afghanistan</li>
.......
如果我使用硒IDE錄製的動作,從下面的腳本被記錄在列表中選擇「聯合王國」。運行代碼片段以查看其中的命令。
<table border="1">
<tr>
<td>Command</td>
<td>Target</td>
</tr>
<tr>
<td>click</td>
<td>css=a.chzn-single > span</td>
</tr>
<tr>
<td>click</td>
<td>id=phMainContent_EmployeeAdd1_ddlCountry_chzn_o_1</td>
</tr>
</table>
我可以在IDE中重複運行此腳本和英國從下拉每次選擇。但是,如果我出口的C#/ NUnit的/ webdriver的代碼下面
driver.FindElement(By.CssSelector("a.chzn-single > span")).Click();
driver.FindElement(By.Id("phMainContent_EmployeeAdd1_ddlCountry_chzn_o_1")).Click();
並執行它,它失敗與硒元素不可見例外一號聲明。
有關如何解決此問題的任何建議?
這可能是一個時間問題之前,爲了確保下拉是可見的。嘗試插入一個Thread.sleep(2000);聲明之間。請注意,該聲明採用Java語言。 – rs79