How do I know or find the xpath for table? Or, even better, get the xpath for all tables in a specific page?

I'm getting no_such_element when running:

    public void test() {

        String tableID = p.getProperty("tableID");


        WebElement Webtable = driver.findElement(By.id("TableID")); // Replace TableID with Actual Table ID or Xpath
        List<WebElement> TotalRowCount = Webtable.findElements(By.xpath("//*[@id='" + tableID + "']/tbody/tr"));

        log.info("No. of Rows in the WebTable: " + TotalRowCount.size());
        // Now we will Iterate the Table and print the Values   
        int RowIndex = 1;
        for (WebElement rowElement : TotalRowCount) {
            List<WebElement> TotalColumnCount = rowElement.findElements(By.xpath("td"));
            int ColumnIndex = 1;
            for (WebElement colElement : TotalColumnCount) {
                log.info("Row " + RowIndex + " Column " + ColumnIndex + " Data " + colElement.getText());
                ColumnIndex = ColumnIndex + 1;
            RowIndex = RowIndex + 1;


general context:




1 Answer 1


If you want all tables then just grab the tables like findElements(By.xpath("//table")); and pull all the tables back. You can step through the list and then adjust to the table ID in question and add the tr for a new list of rows like findElements(By.xpath("//table[@id='table_id']//tr")).

In general that looks like what you are trying for. Not all tables have the tbody tag in them so it's best to just xquery search // inside the table for each row/cell (tr/td) that you want and avoid using full xpath.

  • How do I get a list of all the table id s? What about tables with out table id?
    – Thufir
    Jul 20, 2017 at 3:36
  • You grab the id's by looping through. If you don't have an id choose a different criteria that is unique. Without the HTML it's hard to say what is unique, but whatever uniquely identifies the table is the goal. You can check id and then something else if you need to while you loop.
    – mutt
    Jul 20, 2017 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.