选择基于 Selenium 中的字符串的复选框

Selecting a checkbox based on a string in Selenium

在旁边有一个复选框的 table 中输入一个字符串后,我想单击该复选框。在 selenium 中,我如何遍历 table 并搜索特定文本,然后选中它旁边的复选框。

这是 table 的 html:

<tbody>
  <tr class="keyword-list-item">
    <td width="75%">
      <input class="keyword-selection-checkbox" type="checkbox" data-id="gw_78669090303"/>
      <span>+spatspatulalas</span>
    </td>

    <td width="25%" style="text-align: right; padding-right: 4px;">
      <span class="icon iconGoogle"/>
    </td>
  </tr>

  <tr class="keyword-list-item">
    <td width="75%">
      <input class="keyword-selection-checkbox" type="checkbox" data-id="gw_102731166303"/>
      <span>12.10 test post</span>
    </td>
    <td width="25%" style="text-align: right; padding-right: 4px;">
      <span class="icon iconGoogle"/>
    </td>
  </tr>

您可以为此使用 xpath。只需要稍微聪明一点就可以编写 xpath。注意下面的 xpath 使用它的文本找到复选框。

String text = "12.10 test post";
By xpath = By.xpath("//span[contains(text(),'" + text + "')]/../input");
WebElement element = driver.findElement(xpath);