{"id":3374,"date":"2016-01-05T01:04:00","date_gmt":"2016-01-05T06:04:00","guid":{"rendered":"http:\/\/qxf2.com\/blog\/?p=3374"},"modified":"2017-07-06T14:40:50","modified_gmt":"2017-07-06T18:40:50","slug":"getting-started-with-xpaths","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/getting-started-with-xpaths\/","title":{"rendered":"Getting started with XPaths"},"content":{"rendered":"<p>Assuming you already know what you want to automate and that you have a good reason to automate it, then GUI automation is largely about:<br \/>\na) identifying a web element you want to interact with<br \/>\nb) performing an action on the identified element<br \/>\nc) repeat steps a),b) until you have automated the workflow that you identified.<\/p>\n<p>To perform step a), all GUI automation tools use locators. A locator effectively tells your automation to look for &#8220;a big blue button with the label Confirm on it&#8221; or &#8220;a table cell with the word Qxf2 in it&#8221;. For web applications, <a href=\"https:\/\/en.wikipedia.org\/wiki\/XPath\">XPaths <\/a>and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Cascading_Style_Sheets\"> CSS <\/a> selectors are used to locate web elements. This post helps you get started with writing good XPaths.<\/p>\n<p>XPath is used to locate a web element based on its XML(Extensible Markup Language) path. XPaths can be created in two ways: Absolute XPath and Relative XPath. Absolute XPaths are very susceptible to changes in the web page whereas Relative XPaths can be made more robust.This post focuses on Relative Path, henceforth every instance of XPath here would mean only Relative paths.<\/p>\n<hr \/>\n<h3>Why this post?<\/h3>\n<p>The foundation to robust GUI automated checks is writing good element locators. While talking to testers, we noticed that their habits around writing element locators have not evolved since they first learnt how to write them. Most testers just learn to write locators one way when they start learning GUI automation and then stick to it. Many bad habits that may be ok when getting started, persist through the tester&#8217;s career. So we thought we would write a series of posts to help you build good fundamentals for identifying elements for your GUI automation.<\/p>\n<hr \/>\n<h3>XPath structure<\/h3>\n<p>An XPath:<br \/>\na) starts with a \/\/<br \/>\nb) is followed by a HTML element<br \/>\nc) the HTML element is identified by a condition within square brackets<br \/>\nc1) the condition involves the attribute(s) of the HTML element<br \/>\nc2) the attribute is referenced by the @ symbol<br \/>\nc3) the condition can have logical operators like NOT, AND, OR, contains, =, etc.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/Table.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/Table.png\" alt=\"Constructing a basic XPath\" width=\"1202\" height=\"900\" class=\"aligncenter size-full wp-image-3461\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/Table.png 1202w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/Table-300x225.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/Table-1024x767.png 1024w\" sizes=\"auto, (max-width: 1202px) 100vw, 1202px\" \/><\/a><\/p>\n<p>Constructing a good XPath involves finding a html-element, locating it uniquely based on its attributes and then if needed, deriving a path referencing it to another element which is less likely to change in a web page. We have tackled this more advanced version in <a href=\"https:\/\/qxf2.com\/blog\/xpath-tutorial\/\">this post<\/a>.<\/p>\n<hr \/>\n<h3>Examples<\/h3>\n<p>We are going to use the elements in the page <a href=\"http:\/\/qxf2.com\/selenium-tutorial-main\">http:\/\/qxf2.com\/selenium-tutorial-main<\/a> to clearly explain arriving at good XPaths. Let us try and create Xpaths for:<br \/>\nExample 1. the <strong>name field <\/strong> in the form<br \/>\nExample 2. the submit button in the form with text <strong>Click me!<\/strong><br \/>\nExample 3. the cell with text <strong>michael@example.com<\/strong> in the Example Table. <\/p>\n<p>Example 1.<br \/>\nOpen the page with firefox or chrome, right click on the <strong>name field<\/strong> in the form and select <strong>Inspect Element<\/strong><br \/>\n<a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/xpath_blog_namefield.png\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/xpath_blog_namefield.png\" alt=\"xpath_blog_namefield\" width=\"1352\" height=\"488\" class=\"aligncenter size-full wp-image-3429\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/xpath_blog_namefield.png 1352w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/xpath_blog_namefield-300x108.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/xpath_blog_namefield-1024x370.png 1024w\" sizes=\"auto, (max-width: 1352px) 100vw, 1352px\" \/><\/a><\/p>\n<p><strong>Note<\/strong>: We have tried arriving on an XPath from the basic syntax <strong> \/\/html element[@attribute = &#8216;value&#8217;]<\/strong> adding element,attribute and value with each step<\/p>\n<p>the first step in writing an XPath is identifying the html element ,<strong>input<\/strong> with unique attributes represent the element in the web page,<\/p>\n<pre lang='python'>\/\/input[@attribute = 'value']<\/pre>\n<p>in the next step an attribute is chosen for the html element,the <strong> id <\/strong> attribute is chosen for this example as it is unique<\/p>\n<pre lang=\"python\"> \/\/input[@id = 'value']<\/pre>\n<p>in the final step we add the value &#8216;name&#8217; to its attribute(id)<\/p>\n<pre lang=\"python\"> \/\/input[@id = \u2018name\u2019]<\/pre>\n<p>thus writing an XPath for the name field in this page is straightforward as it is represented by a html element with unique attributes. <\/p>\n<p>Excample 2.<br \/>\nTo inspect <strong>click me!<\/strong> right click on it and select <strong>Inspect Element<\/strong><br \/>\n<a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/xpath_blog_clickme.png\" data-rel=\"lightbox-image-2\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/xpath_blog_clickme.png\" alt=\"xpath_blog_clickme\" width=\"974\" height=\"657\" class=\"aligncenter size-full wp-image-3436\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/xpath_blog_clickme.png 974w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/xpath_blog_clickme-300x202.png 300w\" sizes=\"auto, (max-width: 974px) 100vw, 974px\" \/><\/a><\/p>\n<p>The html element for this example is <strong> button <\/strong><\/p>\n<pre lang='python'>\/\/button[@attribute = 'value']<\/pre>\n<p>the attributes for the button element are class and type, we will use a different attribute for this example as we assume the  values of class and type may change<\/p>\n<pre lang=\"python\"> \/\/button[contains(text(),'value')]<\/pre>\n<p>The value for the attribute text() can be a phrase that is used to represent the element<\/p>\n<pre lang=\"python\"> \/\/button[contains(text(),'Click me!')]<\/pre>\n<p>Contains() operator  can be used if a particular text is unlikely to be changed,the name of the button is going to remain &#8216;Click me!&#8217;<\/p>\n<p>Example 3.<br \/>\n To inspect <strong>michael@example.com<\/strong> right click on it and select <strong>Inspect Element<\/strong><\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/xpath_blog_td3.png\" data-rel=\"lightbox-image-3\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/xpath_blog_td3.png\" alt=\"xpath_blog_td\" width=\"976\" height=\"250\" class=\"aligncenter size-full wp-image-3418\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/xpath_blog_td3.png 976w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/xpath_blog_td3-300x77.png 300w\" sizes=\"auto, (max-width: 976px) 100vw, 976px\" \/><\/a><\/p>\n<p>The html-element for this example can be the <strong>table<\/strong> as its name attribute seems fixed.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/xpath_blog_table4.png\" data-rel=\"lightbox-image-4\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/xpath_blog_table4.png\" alt=\"xpath_blog_table\" width=\"975\" height=\"251\" class=\"aligncenter size-full wp-image-3419\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/xpath_blog_table4.png 975w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/xpath_blog_table4-300x77.png 300w\" sizes=\"auto, (max-width: 975px) 100vw, 975px\" \/><\/a><\/p>\n<p>unlike the other two examples the XPath for this element has to be derived from a html element above it as the element that holds <strong>michael@example.com<\/strong> has no unique attributes.<\/p>\n<p>the closest html element is <strong>table<\/strong><\/p>\n<pre lang=\"python\">\/\/table[@attribute = 'value']<\/pre>\n<p>the attributes of the html element are class and name, like the previous examples we have chosen an attribute that is unlikely to change<\/p>\n<pre lang=\"python\"> \/\/table[@name = 'value']<\/pre>\n<p>the value of the name attribute is \u2018Example Table\u2019<\/p>\n<pre lang=\"python\"> \/\/table[@name = \u2018Example Table\u2019]<\/pre>\n<p>with the html-element set and defined, the element can be arrived at in two different ways<\/p>\n<pre lang=\"python\">(a) \/\/table[@name = 'Example Table']\/tbody\/tr[1]\/td[2]<\/pre>\n<pre lang=\"python\">(b) \/\/table[@name = 'Example Table']\/descendant::td[contains(text(),'michael@example.com')]<\/pre>\n<p>the XPath in example (a) is derived from the table html element to the corresponding element using indices whereas the XPath in example (b) we use the descendant (<a href=\"http:\/\/www.w3schools.com\/xsl\/xpath_axes.asp\">XPath axis<\/a>)to navigate to the element.<br \/>\nthough the examples a and b would find the intended elements, the example (a) is usually not preferred since the chances of re-ordering the data in the table are high ,i.e any other element could take the place of michael@example.com and it could be pushed to a newer location . The descendant  is used to make our locator robust to any changes that can be made to data in the rows of the table.<\/p>\n<hr \/>\n<h3>Tools:<\/h3>\n<p>You can check if the XPath you constructed identifies the element you want or not by using a couple of useful tools.<\/p>\n<p><strong>1. Firebug<\/strong>&#8211; <a href = \"https:\/\/addons.mozilla.org\/en-US\/firefox\/addon\/firebug\/\">Firebug<\/a> is a Firefox plugin. Firebug provides access to a plethora of developer tools that supports editing, debugging and monitoring CSS, HTML and JavaScript live on any webpage. It assists in writing XPaths as it supports inspecting and editing HTML attributes and their values. For additional information on Firebug please refer <a href=\"https:\/\/qxf2.com\/blog\/firebug-2-0-to-find-xpaths\/\">Using Firebug 2.0 to craft XPaths<\/a><\/p>\n<p><strong>2. CSS and XPath checker<\/strong>&#8211; <a href = \"https:\/\/chrome.google.com\/webstore\/detail\/css-and-xpath-checker\/aoinfihhckpkkcpholfhmkeplbhddipe?hl=en\"> CSS and XPath checker<\/a> is add-on for Chrome to help you verify your XPaths.<br \/>\n<a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/css_xpath_selector_match.png\" data-rel=\"lightbox-image-5\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/css_xpath_selector_match.png\" alt=\"CSS and XPath selector in Chrome\" width=\"1277\" height=\"605\" class=\"aligncenter size-full wp-image-3422\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/css_xpath_selector_match.png 1277w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/css_xpath_selector_match-300x142.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/12\/css_xpath_selector_match-1024x485.png 1024w\" sizes=\"auto, (max-width: 1277px) 100vw, 1277px\" \/><\/a><\/p>\n<hr \/>\n<p>Hope this helps get you started with XPaths. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Assuming you already know what you want to automate and that you have a good reason to automate it, then GUI automation is largely about: a) identifying a web element you want to interact with b) performing an action on the identified element c) repeat steps a),b) until you have automated the workflow that you identified. To perform step a), [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[],"class_list":["post-3374","post","type-post","status-publish","format-standard","hentry","category-xpath"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/3374","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=3374"}],"version-history":[{"count":66,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/3374\/revisions"}],"predecessor-version":[{"id":6379,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/3374\/revisions\/6379"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=3374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=3374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=3374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}