我有一个下拉列表在一个网页上,用于选择一个已经使用jQuery选择的插件呈现的国家。以下html的摘录,
<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>
.......如果我使用Selenium记录从列表中选择“联合王国”的操作,则会记录以下脚本。运行代码段以查看包含命令的表。
<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(); 并执行它,它将在第1条语句上失败,其中Selenium 元素不可见异常。
关于如何解决这个问题,有什么建议吗?
发布于 2016-03-04 17:02:15
您可以尝试xPath并选择类似于//span[contains(.,'Please Select')]
发布于 2016-03-04 20:17:45
使用显式等待以确保下拉列表在单击之前是可见的。
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement dropdown = wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("a.chzn-single > span")));
dropdown.Click();
driver.FindElement(By.Id("phMainContent_EmployeeAdd1_ddlCountry_chzn_o_1")).Click(); https://stackoverflow.com/questions/35801688
复制相似问题