我有一个带有“Foo”链接的表,一开始它总是 20。 单击链接后,我可能会出现一个按钮。
我想编写一个程序,涵盖这两个功能:
点击第一个“Foo”链接:
- 如果按钮可见,请单击它,返回,然后再次单击第一个“Foo 链接”。点击后旧的消失了,但底部会添加一个新的,
- 如果该按钮不可见,请返回并选择下一行中的“Foo”链接(添加以抵消偏移量)。
我尝试使用以下代码:
List<WebElement> elements = driver.findElements(By.linkText("Never"));
System.out.println(driver.findElements(By.linkText("Foo")).size());
int nextRow=0;
int uncleanedSubmissions = 0;
int cleanedSubmissions = 0;
System.out.println(nextRow);
do {
elements.get(nextRow).click();
Helper.sleep(3000, driver);
if (driver.findElement(By.className("button")).isDisplayed()) {
driver.findElement(By.className("button")).click();
driver.get(baseUrl);
cleanedSubmissions++;
System.out.println(nextRow);
} else if (!(driver.findElement(By.className("button").isDisplayed())) {
uncleanedSubmissions++;
nextRow++;
System.out.println(uncleanedSubmissions + ". submission has not been cleaned");
driver.get(baseUrl);
}
} while (nextRow < 20);
它被简化以防止长类名和等待线,这在这里并不重要。
当我执行此代码时,出现错误:java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
在我看来,因为我尝试错误地单击“Foo”链接(elements.get(nextRow).click();
无法以这种方式使用 nextRow)。
应该如何实现?
HTML 代码:
<table class="taglib-search-iterator" data-searchcontainerid="_151_workflowInstancesSearchContainer">
<tbody>
<tr class="portlet-section-header results-header">
<tr class="lfr-template portlet-section-body results-row">
<tr class="portlet-section-body results-row">
<tr class="portlet-section-alternate results-row alt">
<tr class="portlet-section-body results-row">
<tr class="portlet-section-alternate results-row alt">
<td id="col-end-date_row-5" class="align-left">
<a href="FooLink">Foo</a>
</td>
</tr>
<tr class="portlet-section-body results-row">
<tr class="portlet-section-alternate results-row alt">
<tr class="portlet-section-body results-row">
<tr class="portlet-section-alternate results-row alt">
<td id="col-end-date_row-9" class="align-left">
<a href="FooLink">Foo</a>
</td>
</tr>
<tr class="portlet-section-body results-row">
<tr class="portlet-section-alternate results-row alt">
<tr class="portlet-section-body results-row">
<tr class="portlet-section-alternate results-row alt">
<tr class="portlet-section-body results-row">
<tr class="portlet-section-alternate results-row alt">
<tr class="portlet-section-body results-row">
<tr class="portlet-section-alternate results-row alt">
<tr class="portlet-section-body results-row">
<tr class="portlet-section-alternate results-row alt">
<tr class="portlet-section-body results-row">
<tr class="portlet-section-alternate results-row alt last">
</tbody>
</table>
其他 tr
完全相同 - 一个针对示例进行了扩展。
带有按钮的页面示例只是(排除其他 css 问题,没有 iframe):
<a id="button" class="taglib-icon" href="Linkhere">
但是此页面不包含任何其他重要内容。
编辑2:
我已将代码更改为上述内容 - 第一次运行时出现错误 StaleElementReferenceException: The element reference is stale.
,它指向 elements.get(nextRow).click();
(得到1.提交尚未清理
日志) - 第一次运行是第二种情况
我会继续努力解决这个问题
最佳答案
由于您的列表大小为 20
int Number = driver.findElements(By.linkText("Foo")).size();
您应该将 while 条件的代码更改为
while (nextRow<20);
相反
while (nextRow<=20);
关于java - 在 Selenium 中选择不同行中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43183842/