我的下拉列表包含300多个选项。当我尝试通过webdriver单击任何选项时,要么我选择了错误的选项,要么获得该元素不可见的异常。我附加在HTML和webdriver代码下面。希望大家尽快解决我的疑问。在此先感谢。
<New>
<From>
<Scale>Seven</Scale>
<ScaleName>Height</ScaleName>
</From>
</New>
<New>
<From>
<Scale>Ten</Scale>
<ScaleName>Height</ScaleName>
</From>
</New>
<New>
<From>
<Scale>***XXX***</Scale>
<ScaleName>Height</ScaleName>
</From>
</New>
<New>
<From>
<Scale>***XXX***</Scale>
<ScaleName>Height</ScaleName>
</From>
</New>
<New>
<From>
<Scale>Tony</Scale>
<ScaleName>Name</ScaleName>
</From>
</New>
<New>
<From>
<Scale>Tom</Scale>
<ScaleName>Name</ScaleName>
</From>
</New>
<New>
<From>
<Scale>***XXX***</Scale>
<ScaleName>Name</ScaleName>
</From>
</New>
<New>
<From>
<Scale>***XXX***</Scale>
<ScaleName>Name</ScaleName>
</From>
</New>
&#13;
<div class="MenuOuter" armenuelementserial="0" style="z-index: 100007; left: 712px; width: 388px; height: 266px; top: 13px; visibility: inherit;">
<div class="MenuScrollUp" style="background-image: url("../../../../resources/images/menu_up.gif"); width: 386px; visibility: inherit; height: 20px;"></div>
<div class="MenuTableContainer" style="top: 20px;">
<table class="MenuTable" style="width: 386px;" cellspacing="0" cellpadding="0">
<tbody class="MenuTableBody">
<tr class="MenuTableRow">
<td class="MenuEntryName" nowrap=""> i i </td>
<td class="MenuEntryNoSub" arvalue=" i i "></td>
</tr>
<tr class="MenuTableRow">
<td class="MenuEntryName" nowrap="">102765-ASSET1EC</td>
<td class="MenuEntryNoSub" arvalue="102765-ASSET1EC"></td>
</tr>
<tr class="MenuTableRow">
<td class="MenuEntryName" nowrap="">4G_SC01_CUS</td>
<td class="MenuEntryNoSub" arvalue="4G_SC01_CUS"></td>
</tr>
<tr class="MenuTableRow">
<tr class="MenuTableRow">
<tr class="MenuTableRow">
<tr class="MenuTableRow">
<tr class="MenuTableRow">
<tr class="MenuTableRow">
<tr class="MenuTableRow">
<tr class="MenuTableRow">
<tr class="MenuTableRow">
<tr class="MenuTableRow">
<tr class="MenuTableRow">
<tr class="MenuTableRow">
<tr class="MenuTableRow">
<tr class="MenuTableRow">
<tr class="MenuTableRow">
</tbody>
</table>
</div>
<div class="MenuScrollDown" style="background-image: url("../../../../resources/images/menu_down.gif"); width: 386px; visibility: inherit; height: 20px; top: 244px;"></div>
</div>
&#13;
答案 0 :(得分:1)
在点击元素之前,将元素滚动到视图中。请尝试以下代码。
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true)",element);
element.click()
此外,您可以通过使用通用xpath来避免遍历所有元素,如下所示
WebElement element = driver.findElements(By.xpath("//table[@class='MenuTable']/descendant::td[contains(text(),'ELEMENT TO BE CLICKED')]")); //this will find the tablecolumn with text ELEMENT TO BE CLICKED.