Selenium webdriver遍历行并仅检索某些列

时间:2016-11-10 09:36:03

标签: java selenium selenium-webdriver

我的tbodytrtdtrtr

我希望遍历.text()并获取仅td的{​​{1}}。 以下是我在tr s中分配到ListalertsTableRowsList

的部分
for (int i = 0; i < alertsTableRowsCount; i++){     

            String reference = alertsTableRowsList.get(i).findElement(By.xpath("//td[@data-title-text = 'Reference']")).getText(); //td number 3
            String classification = alertsTableRowsList.get(i).findElement(By.xpath("//td[@data-title-text = 'Classification']")).getText(); //td number 4
            String description = alertsTableRowsList.get(i).findElement(By.xpath("//td[@data-title-text = 'Description']")).getText(); //td number 7
            String status = alertsTableRowsList.get(i).findElement(By.xpath("//td[@data-title-text = 'Status']")).getText(); //td number 8
            String date = alertsTableRowsList.get(i).findElement(By.xpath("//td[@data-title-text = 'Date']")).getText(); //td number 9
            String owner = alertsTableRowsList.get(i).findElement(By.xpath("//td[@data-title-text = 'Owner']")).getText(); //td number 10

            System.out.println("reference : " + reference);
            System.out.println("classification : " + classification);
            System.out.println("description : " + description);
            System.out.println("status : " + status);
            System.out.println("date : " + date);
            System.out.println("owner : " + owner); 

        }

我注意到只返回了第一个.text()td的{​​{1}}。在这种情况下,它会被返回两次,因为在这种情况下,我tr中只有两个tr

输出:

tbody

tbody的HTML:

reference : AML_AC.20161102.12
classification : AML Central Alert
Wholesale - Wire transfer
description : Thoughtbeat Ltd
status : Analysis
date : 11/02/2016
owner : Strydom, AJ (Rico)

reference : AML_AC.20161102.12
classification : AML Central Alert
Wholesale - Wire transfer
description : Thoughtbeat Ltd
status : Analysis
date : 11/02/2016
owner : Strydom, AJ (Rico)

1 个答案:

答案 0 :(得分:1)

如果alertsTableRowsList是您的WebElements列表,那么您可以像这样迭代:

 for (int i = 0; i < alertsTableRowsList.size(); i++){     
    String reference = driver.findElement(By.xpath("//tbody/tr[" + (i + 1) + "]/td[@data-title-text = 'Reference']")).getText();
    String classification = driver.findElement(By.xpath("//tbody/tr[" + (i + 1) + "]/td[@data-title-text = 'Classification']")).getText(); //td number 4
    String description = driver.findElement(By.xpath("//tbody/tr[" + (i + 1) + "]/td[@data-title-text = 'Description']")).getText(); //td number 7
    String status = driver.findElement(By.xpath("//tbody/tr[" + (i + 1) + "]/td[@data-title-text = 'Status']")).getText(); //td number 8
    String date = driver.findElement(By.xpath("//tbody/tr[" + (i + 1) + "]/td[@data-title-text = 'Date']")).getText(); //td number 9
    String owner = driver.findElement(By.xpath("//tbody/tr[" + (i + 1) + "]/td[@data-title-text = 'Owner']")).getText(); //td number 10

    System.out.println("reference : " + reference);
    System.out.println("classification : " + classification);
    System.out.println("description : " + description);
    System.out.println("status : " + status);
    System.out.println("date : " + date);
    System.out.println("owner : " + owner); 
}