简体   繁体   English

使用Selenium WebDriver查找表行号

[英]Find a table row number using selenium webdriver

I've got a table and need to find a specific row number. 我有一张桌子,需要找到一个特定的行号。 In this case, I'm interested in the 2nd row (2nd tr). 在这种情况下,我对第二行(2tr)感兴趣。

Here's the HTML: 这是HTML:

<table>
  <thead>
    <tbody>
      <tr class="classes mico_models_classes_7 listRowWhite">
      <tr class="classes mico_models_classes_8 listRowDark">
         <td style="width:130px;">9:00am - 10:30am</td>
         <td class="noprint enrolledFull" style="width:70px;height:40px;text-align:center;">   </td>
         <td style="width:200px;">
         <a class="eventName" data-eventdescription="Very long spin class" data-eventname="Spinning 90 min" href="javascript://">Spinning 90 min</a>
        <br>
       with
       <a class="classesEmployeeName" title="John Doe" href="javascript://" data-employeeid="5117">John Doe</a>
      </td>
    <td style="text-align:right;width:72px;">0 of 20</td>
   <td>Spin Class</td>
  </tr>
  <tr class="classes mico_models_classes_9 listRowWhite">
  <tr class="classes mico_models_classes_10 listRowDark">
 </tbody>
</table>

Unfortunately, the following returns null for dataRowIndex : 不幸的是,以下代码为dataRowIndex返回null:

String classTittle = "Spinning 90 min";
String dataRowIndex = driver.findElement(By.cssSelector("[data-eventname='" + classTitle + "']")).getAttribute("rowIndex");

You can solve this by iterating the tr HTML-elements while counting them. 您可以通过在对tr HTML元素进行计数的同时对其进行迭代来解决此问题。 You can also access them while iterating. 您还可以在迭代时访问它们。

List<WebElement> element = driver.findElements(By.cssSelector("tr"));

        int row = 0;

        for( WebElement w : element){

            String elemText = w.getText();

            System.out.println(elemText);

            String clickText = "Spinning 90 min";

            if(elemText.contains(clickText)){

                w.click(); //do something with the element

                System.out.println("Text in row " + row + " is " + clickText + " so i clicked it!");
            }

            System.out.println("this was row " + row + "\n");

            row++;
        }

Will yield: 将产生:

this was row 0

9:00am - 10:30am Spinning 90 min
with John Doe 0 of 20 Spin Class
Text in row 1 is Spinning 90 min so i clicked it!
this was row 1


this was row 2


this was row 3

You might want to encapsulate your specific logic in a method later on. 您稍后可能希望将特定的逻辑封装在一个方法中。 Hope this helps ^^-d 希望这可以帮助^^-d

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

相关问题 如何使用Selenium Webdriver Java查找表行号 - How to find the table row number using Selenium Webdriver Java 硒webdriver获取表中的可见行号 - selenium webdriver to get visible row number in table 如何遍历表以查找第 1 列中的字符串匹配项,然后使用 Selenium WebDriver Java 在另一列的同一行上选择下拉列表? - How to loop through table to find a string match in column 1, then select a dropdown on the same row in another column using Selenium WebDriver Java? 使用Selenium WebDriver查找元素 - Find an element using selenium WebDriver 硒webdriver找不到正确数量的元素 - selenium webdriver does not find the correct number of elements 如何使用 Selenium Webdriver - Java 查找网页上的复选框总数? - How to find the total number of checkboxes present on web page using Selenium Webdriver - Java? 如何使用硒webdriver查找是否启用了元素? - How to find an element is enabled or not using selenium webdriver? 使用Selenium Webdriver无法找到元素 - Cannot find element using Selenium Webdriver 如何使用Selenium WebDriver查找搜索结果 - how to find search results using selenium webdriver 如何使用 Selenium Webdriver 和 Java 在同一行中单击旁边/上一个表格单元格(TD)? - How to click on the aside / previous Table Cell (TD) in the same row using Selenium Webdriver with Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM