在 Selenium 中选择不同行中的按钮

Choosing the button in different rows in Selenium

我有一个 table 和 "Foo" link,一开始它总是 20。 单击 link 后,我可能会有一个按钮。

我想写一个程序,涵盖这两个功能:

单击第一个 "Foo" link 和:

  1. 如果按钮可见,请单击它,返回,然后再次单击第一个 "Foo link"。点击后旧的消失了,但在底部会添加一个新的,
  2. 如果按钮不可见,返回并选择下一行中的 "Foo" link(添加以抵消偏移量)。

我尝试使用以下代码:

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" link(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. submission has not been cleaned 日志) - 第一个 运行是第二种情况 我会继续努力解决它

因为您的列表大小是 20

int Number = driver.findElements(By.linkText("Foo")).size();

您应该将 while 条件的代码更改为

while (nextRow<20);

而不是

while (nextRow<=20);