【发布时间】:2016-03-27 18:13:03
【问题描述】:
我使用以下代码来检索我想在 selenium 中选择的行:
WebElement row = driver.findElement(By.xpath("//tr[contains(.,'prod1')]"));
结果,我得到以下行:
<th width="18px">
<input class="selectedProducts" id="checkbox-37640" onclick="markedProductsHandler.toggleMarkProduct(this,37640)" type="checkbox">
</th>
<th style="white-space:nowrap; width:573px;" align="left">
<a class="maxTextSize" style="width:530px" href="product.html?id=37640&rm7zz5c=ZBOeBCOAs">aa prod1</a>
</th>
<th style="white-space:nowrap;" align="right" width="140px">
<a href="product-edit.html?id=37640&rm7zz5c=ZBOeBCOAs"><img src="_images/icons/pencil.png?rm7zz5c=ZBOeBCOAs" title="Edit"></a>
<input name="favoriteid" class="favoriteAction" value="37640" type="hidden">
<input name="favoriteaction" class="favoriteAction" value="add" type="hidden">
<a href="javascript:" onclick="onFavoriteAction(this)">
<img src="_images/icons/star_grey.png?rm7zz5c=ZBOeBCOAs" alt="Watch product" title="Watch product">
</a>
<form action="product-pdf.html?rm7zz5c=ZBOeBCOAs" method="post">
<input name="export" value="pdf" type="hidden">
<input name="id" value="37640" type="hidden">
<a href="javascript:" onclick="javascript: $(this).parent().submit();"><img src="_images/icons/page_white_acrobat.png?rm7zz5c=ZBOeBCOAs" title="Download as PDF"></a>
</form>
</th>
现在我想选中复选框:
row.findElement(By.xpath("//input[@type='checkbox']")).click();
但我收到以下错误:
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
有人可以帮我吗?谢谢。
【问题讨论】:
-
根据异常,似乎复选框被隐藏了。类
selectedProducts有visibility:hidden;或display:none之类的样式吗?github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/… -
你能告诉我们
row.findElement(By.xpath("//input[@type='checkbox']")).isDisplayed()的结果吗? + 尝试添加等待复选框可见的服务员 -
您好,结果为假
-
在您的示例 HTML 中,
tr到底在哪里?一切都是th。 -
是的,它们看起来像这样,selenium 找到并返回这个 html。不好吗?
标签: java html selenium testing checkbox