简体   繁体   中英

Java Selenium multiple table structure, iterate through all

I've got a table with rows inside a row of another table which has many rows (other tables with rows).

Wow. That's confusing. Let me lay it out in front of ya!

.//*[@id='chatComponent']/div/swx-navigation
/div/div/div/div[2]/div[1]/div/ul/li[*]/div/ul/li[*]

The firstTable & rows starts here:

 firstTable = /div/div/div/div[2]/div[1]/div/ul/li[*]

The secondTable & rows starts here:

 secondTable = firstTable/div/ul/li[*]

Here is my code where I am experimenting how to System.out.print all each of the items in the tables (which are within the first table)

    List<WebElement> liElements = driver.findElements(By
            .xpath(".//*[@id='chatComponent']/div/swx-navigation/div/div/div/div[2]/div[1]/div/ul/li[*]"));
    List<WebElement> liElements2 = driver.findElements(By
            .xpath(".//*[@id='chatComponent']/div/swx-navigation/div/div/div/div[2]/div[1]/div/ul/li[*]/div/ul/li[*]"));

    System.out.println(liElements.size());
    System.out.println(liElements2.size());

    for (int i = 1; i < liElements.size()+1; i++) {
    if (i < liElements.size() ){
        WebElement linkElement = driver
                .findElement(By
                        .xpath(".//*[@id='chatComponent']/div/swx-navigation/div/div/div/div[2]/div[1]/div/ul/li[1]/div/ul/li[" + i + "]"));
        System.out.println(linkElement.getText());
    } else if (i == liElements.size()){
         WebElement linkElement2 = driver
                    .findElement(By
                            .xpath(".//*[@id='chatComponent']/div/swx-navigation/div/div/div/div[2]/div[1]/div/ul/li[*]/div/ul/li[" + i + "]"));
        System.out.println(linkElement2.getText());
        }
    }

The problem now is that, It will iterate through the first table of tables until there are no more objects to find and then throws error, but it does not move onto the next table to print objects there. It just says OK, printed all elements in this table, but moves on to look for an additional non-existing element in that same table and fails.

I am asking, how can I achieve this?

Any assistance is greatly appreciated.

You should follow the following algorithm :

  1. Find all the rows in the outer (first) table iterate through the rows.
  2. From each row in the outer table, find the rows in each of the inner table and iterate through the inner table rows.

     // find all the rows in the outer table List<WebElement> outerLIElementList = driver.findElements( By.xpath(".//*[@id='chatComponent']/div/swx-navigation/div/div/div/div[2]/div[1]/div/ul/li")); System.out.println(outerLIElementList.size()); // iterate through the rows in the outer element for (WebElement outerLIElement : outerLIElementList) { // find the inner table rows using the outer table row List<WebElement> innerLIElementList = outerLIElement.findElements(By.xpath("//div/ul/li[*]")); // iterate through the inner table rows and sysout for (WebElement innerLIElement : innerLIElementList) { System.out.println(innerLIElement.getText()); } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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