{"id":2651,"date":"2019-11-25T04:20:33","date_gmt":"2019-11-25T04:20:33","guid":{"rendered":"https:\/\/codoid.com\/xpath-examples-for-html-table\/"},"modified":"2025-03-28T14:24:37","modified_gmt":"2025-03-28T14:24:37","slug":"xpath-examples-for-html-table","status":"publish","type":"post","link":"https:\/\/codoid.com\/selenium-testing\/xpath-examples-for-html-table\/","title":{"rendered":"XPath Examples for HTML Table"},"content":{"rendered":"<p>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 <a href=\"https:\/\/codoid.com\/testing-services\/selenium-testing-services\/\">Selenium Testing Services company<\/a>, we have mitigated several failed Selenium automation testing projects. Most of them failed due to not following test automation best practices, writing absolute <a href=\"https:\/\/codoid.com\/xpath-vs-css-selectors\/\">XPaths &#038; CSS Selectors<\/a>. 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.<\/p>\n<h3>XPath to locate a table using row count<\/h3>\n<p>Sometimes you may need to identify an <a href=\"https:\/\/html.com\/tables\/\" target=\"_blank\" rel=\"noopener noreferrer\">HTML table<\/a> 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&#8217;s see how to identify using its row count.<\/p>\n<p style=\"text-align:center;\"><img decoding=\"async\" title=\"XPath Examples with Count Function\" src=\"https:\/\/codoid.com\/wp-content\/uploads\/2019\/11\/XPath-Examples-with-Count-Function.png\" alt=\"XPath Examples with Count Function\"><\/p>\n<p><strong>XPath<\/strong><\/p>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n\/\/table[count(.\/\/tr)=6]\r\n<\/pre>\n<\/div>\n<h3>locate using column count<\/h3>\n<p>Locating a table using column count is simple. Just add &#8216;th&#8217; in the Row count Xpath.<\/p>\n<p><strong>XPath<\/strong><\/p>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n\/\/table[count(.\/\/tr\/th)=4]\r\n  <\/pre>\n<\/div>\n<h3>Locate Last Row<\/h3>\n<p>You no need to use For Loop to identify last row in a table. Use &#8216;Last&#8217; XPath function to locate last row.<\/p>\n<p style=\"text-align:center;\"><img decoding=\"async\" title=\"XPath Examples with Last Function\" src=\"https:\/\/codoid.com\/wp-content\/uploads\/2019\/11\/XPath-Examples-with-Last-Function.png\" alt=\"XPath Examples with Last Function\"><\/p>\n<p><strong>XPath<\/strong><\/p>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n\/\/table[@id='tbl1']\/\/tr[last()]\r\n  <\/pre>\n<\/div>\n<h3>Identify Alternate Rows<\/h3>\n<p style=\"text-align:center;\"><img decoding=\"async\" title=\"XPath Examples with Alternate Rows\" src=\"https:\/\/codoid.com\/wp-content\/uploads\/2019\/11\/XPath-Examples-with-Alternate-Rows.png\" alt=\"XPath Examples with Alternate Rows\"><\/p>\n<p><strong>CSS<\/strong><\/p>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ntable[id='tbl1'] tr:nth-child(2n+1)\r\n  <\/pre>\n<\/div>\n<h3>Advanced XPath Tricks<\/h3>\n<p><b>Find Data in a Specific Cell<\/b><\/p>\n<p>To target a specific cell based on row and column position:<\/p>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/\/table[@id='tbl1']\/\/tr[3]\/td[2]  \r\n<\/pre>\n<\/div>\n<p><b>Explanation<\/b>: This XPath locates the data in row 3 and column 2 within the table that has an ID tbl1.<\/p>\n<p><b>Find a Cell with Specific Text<\/b><\/p>\n<p>To locate a cell containing exact text:<\/p>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/\/table[@id='tbl1']\/\/td[text()='Sample Data']    \r\n<\/pre>\n<\/div>\n<p><b>Explanation<\/b>: This XPath targets the &#8216;td&#8217; element that contains the exact text &#8220;Sample Data&#8221; in the table with ID tbl1.<\/p>\n<p><b>Find a Cell with Partial Text<\/b><\/p>\n<p>For dynamic content, contains() helps locate text that partially matches:<\/p>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/\/table[@id='tbl1']\/\/td[contains(text(),'Sample')]    \r\n<\/pre>\n<\/div>\n<p><b>Explanation<\/b>: This XPath locates any &#8216;td&#8217; element that contains the word &#8220;Sample&#8221; as part of its text.<\/p>\n<h2>Conclusion:<\/h2>\n<p>Mastering XPath expressions is crucial for effective Selenium automation testing, especially when interacting with HTML tables. By learning to locate tables based on row and column counts, identify specific rows like the last one, and select alternate rows, testers can create more robust and efficient test scripts. At Codoid, we specialize in delivering top-notch automation testing services, ensuring that your applications perform flawlessly across various scenarios. Our expertise in crafting precise XPath expressions and leveraging advanced testing techniques empowers your team to achieve seamless and reliable test automation outcomes.\u200b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 Selenium Testing Services 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":37442,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[187,179,20],"tags":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.3 (Yoast SEO v20.3) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>XPath Examples for HTML Table - Codoid<\/title>\n<meta name=\"description\" content=\"Explore XPath examples for HTML tables, including locating by row and column count, identifying the last row, to enhance automation testing.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"XPath Examples for HTML Table\" \/>\n<meta property=\"og:description\" content=\"Explore XPath examples for HTML tables, including locating by row and column count, identifying the last row, to enhance automation testing.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codoid.com\/learn\/selenium-testing\/xpath-examples-for-html-table\/\" \/>\n<meta property=\"og:site_name\" content=\"Codoid\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codoid.softwaretestingcompany\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-25T04:20:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-03-28T14:24:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codoid.com\/wp-content\/uploads\/2019\/11\/XPath-Examples-for-HTML-Table.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codoid\" \/>\n<meta name=\"twitter:site\" content=\"@codoid\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"XPath Examples for HTML Table - Codoid","description":"Explore XPath examples for HTML tables, including locating by row and column count, identifying the last row, to enhance automation testing.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"XPath Examples for HTML Table","og_description":"Explore XPath examples for HTML tables, including locating by row and column count, identifying the last row, to enhance automation testing.","og_url":"https:\/\/codoid.com\/learn\/selenium-testing\/xpath-examples-for-html-table\/","og_site_name":"Codoid","article_publisher":"https:\/\/www.facebook.com\/codoid.softwaretestingcompany","article_published_time":"2019-11-25T04:20:33+00:00","article_modified_time":"2025-03-28T14:24:37+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/codoid.com\/wp-content\/uploads\/2019\/11\/XPath-Examples-for-HTML-Table.jpg","type":"image\/jpeg"}],"author":"admin","twitter_card":"summary_large_image","twitter_creator":"@codoid","twitter_site":"@codoid","twitter_misc":{"Written by":"admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/codoid.com\/learn\/selenium-testing\/xpath-examples-for-html-table\/#article","isPartOf":{"@id":"https:\/\/codoid.com\/learn\/selenium-testing\/xpath-examples-for-html-table\/"},"author":{"name":"admin","@id":"https:\/\/codoid.com\/#\/schema\/person\/360ee1d38151acb7c746787fbfa8e586"},"headline":"XPath Examples for HTML Table","datePublished":"2019-11-25T04:20:33+00:00","dateModified":"2025-03-28T14:24:37+00:00","mainEntityOfPage":{"@id":"https:\/\/codoid.com\/learn\/selenium-testing\/xpath-examples-for-html-table\/"},"wordCount":453,"commentCount":0,"publisher":{"@id":"https:\/\/codoid.com\/#organization"},"articleSection":["Selenium Testing","Fixed","Blog"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/codoid.com\/learn\/selenium-testing\/xpath-examples-for-html-table\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/codoid.com\/learn\/selenium-testing\/xpath-examples-for-html-table\/","url":"https:\/\/codoid.com\/learn\/selenium-testing\/xpath-examples-for-html-table\/","name":"XPath Examples for HTML Table - Codoid","isPartOf":{"@id":"https:\/\/codoid.com\/#website"},"datePublished":"2019-11-25T04:20:33+00:00","dateModified":"2025-03-28T14:24:37+00:00","description":"Explore XPath examples for HTML tables, including locating by row and column count, identifying the last row, to enhance automation testing.","breadcrumb":{"@id":"https:\/\/codoid.com\/learn\/selenium-testing\/xpath-examples-for-html-table\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codoid.com\/learn\/selenium-testing\/xpath-examples-for-html-table\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codoid.com\/learn\/selenium-testing\/xpath-examples-for-html-table\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codoid.com\/"},{"@type":"ListItem","position":2,"name":"XPath Examples for HTML Table"}]},{"@type":"WebSite","@id":"https:\/\/codoid.com\/#website","url":"https:\/\/codoid.com\/","name":"Codoid","description":"","publisher":{"@id":"https:\/\/codoid.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codoid.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/codoid.com\/#organization","name":"Codoid - Software Testing Company","url":"https:\/\/codoid.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codoid.com\/#\/schema\/logo\/image\/","url":"https:\/\/codoid.com\/wp-content\/uploads\/2020\/09\/Codoid_Software_Testing_Company_Logo.png","contentUrl":"https:\/\/codoid.com\/wp-content\/uploads\/2020\/09\/Codoid_Software_Testing_Company_Logo.png","width":500,"height":500,"caption":"Codoid - Software Testing Company"},"image":{"@id":"https:\/\/codoid.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/codoid.softwaretestingcompany","https:\/\/twitter.com\/codoid","https:\/\/www.instagram.com\/codoid.softwaretestingcompany\/","https:\/\/www.linkedin.com\/company\/codoid-qacompany","https:\/\/www.pinterest.com\/codoid9282\/codoid-software-testing-company\/","https:\/\/www.youtube.com\/channel\/UCCmOTDQgcf4W8oo22mBMUYA"]},{"@type":"Person","@id":"https:\/\/codoid.com\/#\/schema\/person\/360ee1d38151acb7c746787fbfa8e586","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codoid.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/4d33f9036c9d60684b9478f738779823?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4d33f9036c9d60684b9478f738779823?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/codoid.com"]}]}},"_links":{"self":[{"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/posts\/2651"}],"collection":[{"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/comments?post=2651"}],"version-history":[{"count":15,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/posts\/2651\/revisions"}],"predecessor-version":[{"id":37438,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/posts\/2651\/revisions\/37438"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/media\/37442"}],"wp:attachment":[{"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/media?parent=2651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/categories?post=2651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/tags?post=2651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}