简体   繁体   English

根据Selenium中的字符串选择一个复选框

[英]Selecting a checkbox based on a string in Selenium

After Entering a string into a table with a checkbox next to it, I would like to click on the checkbox. 将字符串输入到带有旁边复选框的表中后,我想单击该复选框。 In selenium, how can i iterate through the table and search for a particular text, then check the checkbox next to it. 在硒中,如何遍历表格并搜索特定的文本,然后选中其旁边的复选框。

Here's the html of the 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>

You can use xpath for this. 您可以为此使用xpath Just needs be little smart how you write the xpath. 只是需要聪明一点,如何编写xpath。 Notice the following xpath find the checkbox using the text of it. 请注意,以下xpath使用其文本查找该复选框。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM