XPath Examples for HTML Table


Every automation tester should possess the following two skills while developing an automation test script. 1) Getting to know automation tools 2) locating web elements. As an automation testing company, we have mitigated several failed Selenium automation testing projects. Most of them failed due to not following test automation best practices, writing absolute XPaths & CSS Selectors. Learning Selenium Tool commands is easy. However, writing robust Selenium locators comes from experience. To help newbie test automation engineers, we have written this blog article with various XPath Examples for HTML Tables.

XPath to locate a table using row count

Sometimes you may need to identify an HTML table using its row count. Using XPath count function, you can find a table using row count. The below table has 6 rows including a row for headers. Let's see how to identify using its row count.

XPath Examples with Count Function

XPath

//table[count(.//tr)=6]

locate using column count

Locating a table using column count is simple. Just add 'th' in the Row count Xpath.

XPath

//table[count(.//tr/th)=4]

locate Last Row

You no need to use For Loop to identify last row in a table. Use 'Last' XPath function to locate last row.

XPath Examples with Last Function

XPath

//table[@id='tbl1']//tr[last()]

Identify Alternate Rows

XPath Examples with Alternate Rows

CSS

table[id='tbl1'] tr:nth-child(2n+1)


Leave a Reply

Your email address will not be published. Required fields are marked *