Selenium WebDriver Table Examples
We will see examples on how to count rows / columns of a table using Selenium WebDriver, how to read and print all rows of a table using Selenium WebDriver and more.
We will use below table for our WebDriver Table Examples:
When you open the above html code in a browser, it looks like as below:
Example 1 - WebDriver Table Examples - Counting rows / columns of a table
We are first finding a table using Id. After finding a table we get all rows and columns in separate lists. Using the count method we get the count of rows and columns in a table.
On running the above code below result is shown:
Example 2 - WebDriver Table Examples - Printing all rows of a table
In the foreach loop we are iterating the list and printing each item.
Result of the above code run:
Example 3 - WebDriver Table Examples - Printing a particular cell value
Here we are printing value in the second row and second column of a table. Remember that header column (FirstName, LastName and Age) is considered as first row here.
We are using XPath to get a particular value of a table.
Result: It prints out the value "Carry".
Example 4 - WebDriver Table Examples - Printing all values using XPath
XPath for Jim /html/body/table/tbody/tr[2]/td[1] XPath for Carry /html/body/table/tbody/tr[2]/td[2] XPath for Melina /html/body/table/tbody/tr[3]/td[1]I tried getting XPath for few values as can be seen above. You can observe that XPath in each case remains the same and only the indexes in the tr and td are changing with each XPath. As a result we can use FOR loop and print all the values as below.
Result of running the above code.