In This Tutorial for How to handle web table in Selenium WebDriver, you will learn-

What is a Web Table in Selenium? How to write XPath for Table in Selenium Accessing Nested Tables Using Attributes as Predicates Shortcut: Use Inspect Element for Accessing Tables in Selenium

How to write XPath for Table in Selenium

Consider the HTML code below for handling web tables in Selenium.

We will use XPath to get the inner text of the cell containing the text “fourth cell.”

Step 1 – Set the Parent Element (table) XPath locators in WebDriver always start with a double forward slash “//” and then followed by the parent element. Since we are dealing with web tables in Selenium, the parent element should always be the

tag. The first portion of our Selenium XPath table locator should, therefore, start with “//table”.

Step 2 – Add the child elements The element immediately under

isso we can say thatis the “child” of
. And also,
is the “parent” of. All child elements in XPath are placed to the right of their parent element, separated with one forward slash “/” like the code shown below.

Step 3 – Add Predicates The

element contains twotags. We can now say that these twotags are “children” of. Consequently, we can say thatis the parent of both theelements. Another thing we can conclude is that the twoelements are siblings. Siblings refer to child elements having the same parent. To get to theand not the first. If we simply write “//table/tbody/tr”, then we will be accessing the firsttag. So, how do we access the secondthen? The answer to this is to use Predicates. Predicates are numbers or HTML attributes enclosed in a pair of square brackets “[ ]” that distinguish a child element from its siblings. Since thewe need to access is the second one, we shall use “[2]” as the predicate.

If we won’t use any predicate, XPath will access the first sibling. Therefore, we can access the first

using either of these XPath codes.

The next element we need to access is the second

, and so our XPath should be as shown below.

Remember that when we put the XPath code in Java, we should use the escape character backward slash “\” for the double quotation marks on both sides of “270” so that the string argument of By.xpath() will not be terminated prematurely.

We are now ready to access that cell using the code below.

Shortcut: Use Inspect Element for Accessing Tables in Selenium

If the number or attribute of an element is extremely difficult or impossible to obtain, the quickest way to generate the XPath code is using Inspect Element. Consider the example below from Mercury Tours homepage.

Step 1 Use Firebug to obtain the XPath code.

Step 2 Look for the first “table” parent element and delete everything to the left of it.

Step 3 Prefix the remaining portion of the code with double forward slash “//” and copy it over to your WebDriver code.

The WebDriver code below will be able to successfully retrieve the inner text of the element we are accessing.

Summary

By.xpath() is commonly used to access elements of WebTable in Selenium. If the element is written deep within the HTML code such that the number to use for the predicate is very difficult to determine, we can use that element’s unique attribute instead for Selenium get table element. Attributes are used as predicates by prefixing them with the @ symbol. Use Inspect Element for Accessing WebTable in Selenium

we wish to access (the one with the text “fourth cell”), we must first access the second
. Applying the principles we have learned from steps 2 and 3, we will finalize our XPath code to be like the one shown below.

Now that we have the correct XPath locator, we can already access the cell that we wanted to and obtain its inner text using the code below. It assumes that you have saved the HTML code above as “newhtml.html” within your C Drive.

Accessing Nested Tables

The same principles discussed above applies to nested tables. Nested tables are tables located within another table. An example is shown below.

To access the cell with the text “4-5-6” using the “//parent/child” and predicate concepts from the previous section, we should be able to come up with the XPath code below.

The output below confirms that the inner table was successfully accessed.

Using Attributes as Predicates

If the element is written deep within the HTML code such that the number to use for the predicate is very difficult to determine, we can use that element’s unique attribute instead. In the example below, the “New York to Chicago” cell is located deep into Mercury Tours homepage’s HTML code.

In this case, we can use the table’s unique attribute (width=”270″) as the predicate. Attributes are used as predicates by prefixing them with the @ symbol. In the example above, the “New York to Chicago” cell is located in the first

of the fourth