September 20, 2022 · 5 min · 853 words · Harold Messier
Table of Contents
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
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 two
tags. We can now say that these two
tags are “children” of
. Consequently, we can say thatis the parent of both the
elements.
Another thing we can conclude is that the two
elements are siblings. Siblings refer to child elements having the same parent.
To get to the
we wish to access (the one with the text “fourth cell”), we must first access the second
and not the first. If we simply write “//table/tbody/tr”, then we will be accessing the first
tag.
So, how do we access the second
then? 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 the
we 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
. 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.
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.
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
, 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