0

I have a table with 12 columns, one of the columns is named "quantity" what I need to do is find out which column is the quantitiy one and then get all of the data from the table for that column.

This is some rough code that I have written but at the moment it is just giving me all the data from the table instead of the part that I need.

// Get the table name
WebElement table = driver.findElement(By.className("compact"));

        // Get all the Table headers and look for one the element called quantity.
        List<WebElement> allHeaders = table.findElements(By.tagName("th"));
        System.out.println("This is the total numbers of headers" + allHeaders.size());
        int quantityHead = 0;

        // Get the column where the quantity is listed
        for(WebElement header : allHeaders)
        {
            quantityHead++;
            if(header.getText().equalsIgnoreCase("Quantity"))
            {
                System.out.println(header.getText());
                System.out.println(quantityHead + " is the column number for the quantity");
            }
        }

        // Now get all the TR elements from the table
        List<WebElement> allRows = table.findElements(By.tagName("tr"));
        System.out.println("This is how many allRows: " + allRows.size());

        // This is how many items are in the order item table. number of rows - 1 coz top row is the headings
        int z = allRows.size() - 1;
        System.out.println("This is how many items are in the table : " + z );

        // Work out the cells we need to get by
        //  iterate over all the td
        for (WebElement row : allRows) {

            // now we have all the td
            List<WebElement> cells = row.findElements(By.tagName("td"));

            for (WebElement cell : cells) {

             for(int x=0; x <= allHeaders.size(); x++)
             {
                 int y = x;

                 // if y = the value where the qty header is then output the text of that cell
                 if(y == quantityHead )
                 {
                     System.out.println();
                     System.out.println("The quanity is: " + cell.getText());
                 }
             }
         }
        }
3
  • Well what does it give you? What is printed out? What are you expecting? Against what site? Have you debugged it? Ensured the size of the row collection is what you expect? Commented Oct 11, 2013 at 9:31
  • Has I said it's printing out everything in the table. The site is an internal site so I can't give access to outside the office. In the final if I am checking if y == quantitiyHead so I am only expecting it to print out the cells when y = 7 (which is the qty head) Commented Oct 11, 2013 at 9:37
  • Even if it's an internal site, you can still post some example HTML and output. Commented Oct 15, 2013 at 8:13

1 Answer 1

3

First identify the column position of the quantity in the table. then just find the element by the xpath

WebElement table = driver.findElement(By.className("compact"));

List<WebElement> th=table.findElements(By.tagName("th"));
    int col_position=0;
    for(int i=0;i<th.size();i++){
        if("quantity".equalsIgnoreCase(th.get(i).getText())){
            col_position=i+1;
            break;
        }
    } 
List<WebElement> FirstColumns = table.findElements(By.xpath("//tr/td["+col_position+"]"));
    for(WebElement e: FirstColumns){
        System.out.println(e.getText());
    }    

Hope this helps

Sign up to request clarification or add additional context in comments.

1 Comment

what if it is the first column in that table is the one whose texts needs to be distracted, what does the script turn out to be?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.