Reviewing the values in a table on an HTML page is a crucial step in many automation tests. Selenium provides various locator methods to help navigate through tables, allowing us to determine the number of rows and columns. It also enables us to access any specific element within the table. However, iterating through the rows and columns to look up table values may not always be the most efficient approach, particularly if we aim for faster execution of our automation tests.
Relative Xpath offers an efficient method for looking up table values. Various methods can be used to filter relevant rows and columns, allowing for the optimized retrieval of desired table column values. Below, we demonstrate some techniques on a sample table to extract specific elements from a web page.
Tools used - Google Chrome Inspector
Contains method
To verify the presence of a specific column heading in a table header, the 'contains' method can be utilized to find a match within the DOM. For instance, to ascertain if the column header "Diceret" exists in the table, the following XPath can be used:
//table/thead[contains(tr,'Diceret')]
The inspector locates a match and returns one object as a result. Refer to the image above.
OR Condition
To verify if a table contains rows with specific column values, we can tailor the query to filter for only the desired results. For instance, to ascertain the presence of a row with the value "Apeirian0" in the second column and another row with "Definiebas2" in the fourth column, we would use a locator akin to the one illustrated below.
//table/tbody/tr[contains(td[2],'Apeirian0') or contains(td[4],'Definiebas2')]
The inspector locates a match and returns 2 rows as a result as shown in the image above.
AND Condition
The AND clause guarantees that only the rows satisfying all the conditions linked by the AND clause are retrieved. Specifically, it requires that the values being searched for must exist within the same row. If they do not, no results will be returned. For instance, we can use the AND clause to verify if there is a row where the second column contains "Apeirian0" and the fourth column contains "Definiebas0".
//table/tbody/tr[contains(td[2],'Apeirian0') and contains(td[4],'Definiebas0')]
The inspector returns only one row this time as both the contains conditions return true as shown in the image above.
Starts-with method
The 'starts-with' method is used to determine if a set of rows contains values that begin with a specific string in a specified column. For instance, below we verify the presence of rows with values that commence with the string "Consequuntur" in column 5.
//table/tbody/tr[starts-with(td[5],'Consequuntur')]
The inspector returns 10 matching rows as a result as shown in the image above.
Text method
The text method verifies whether a row contains a specific text value in a designated column. For instance, in the example below, we check for rows with the text value "Consequuntur0" in the fifth column.
//table/tbody/tr/td[(text()='Consequuntur0')]
The inspector returns 1 matching row as a result as shown in the image above.
These strategies assist in filtering through countless rows of data on an HTML page table. Typically, we know what we're searching for, and these techniques enable us to select data much more quickly than sifting through every row, which prolongs loop execution and slows down the test suite. These methods offer numerous applications, provided we employ the necessary logic while navigating the elements of a webpage.
With over 14 years of experience as an automation engineer and a product quality owner, I possess extensive expertise in resolving automation-related challenges. If you encounter quality roadblocks or have business use cases that demand meticulous testing with precision and cost-efficiency, please reach out to me.
You can reach me @
Linkedin - Priyadarshani Pandey | LinkedIn
Comments