{"id":4619,"date":"2014-03-26T13:52:20","date_gmt":"2014-03-26T17:52:20","guid":{"rendered":"http:\/\/www.webperformance.com\/load-testing-tools\/blog\/?page_id=4619"},"modified":"2018-08-09T08:20:05","modified_gmt":"2018-08-09T12:20:05","slug":"xpath-locator-examples","status":"publish","type":"page","link":"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/articles\/real-browser-manual\/building-a-testcase\/how-locate-element-the-page\/xpath-locator-examples\/","title":{"rendered":"XPath locator examples"},"content":{"rendered":"\n<div class=\"landing-page-7mh\">\n    <link href=\"\/css\/how-many-users.css\" rel=\"stylesheet\"\/>\n<div class=\"page-title grid\">\n<h2 class=\"unit\">Web Performance Consulting<\/h2>\n<div class=\"product-actions var--footer\">\n      <span><br \/>\n        Our experts find out how many users <strong>your website<\/strong> can handle!<br \/>\n      <\/span><br \/>\n        &nbsp;<br \/>\n        <a href=\"\/load-testing-consulting-services\/\">Learn More<\/a>\n      <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<p>If you are new to XPath, the power and flexibility of XPath can be a bit bewildering. This page contains some examples to get you started. After that, I recommend the XPath resources linked on our <a href=\"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/?page_id=4623\">Types of Element Locators<\/a> page.<\/p>\n<h2>&#8220;raw&#8221; XPath<\/h2>\n<p>To find the link in this page:<\/p>\n<pre>&lt;html&gt;&lt;body&gt;\r\n&lt;p&gt;The fox jumped over the lazy brown &lt;a href=\"dogs.html\"&gt;dog&lt;\/a&gt;.&lt;\/p&gt;\r\n&lt;\/body&gt;&lt;\/html&gt;<\/pre>\n<p>A raw XPath traverses the hierarchy from the root element of the document (page) to the desired element:<\/p>\n<pre>\/html\/body\/p\/a<\/pre>\n<p>&nbsp;<\/p>\n<h2>Child of Element ID<\/h2>\n<p>XPath can find an element by ID like this:<\/p>\n<pre>\/\/*[@id=\"element_id\"]<\/pre>\n<p>So if you need to find an element that is near another element with an ID, like the link in this example:<\/p>\n<pre>&lt;html&gt;&lt;body&gt;\r\n&lt;p id=\"fox\"&gt;The fox jumped over the lazy brown &lt;a href=\"dogs.html\"&gt;dog&lt;\/a&gt;.&lt;\/p&gt;\r\n&lt;\/body&gt;&lt;\/html&gt;<\/pre>\n<p>you could try an XPath like this to find the first link that is a child of the element with ID=&#8221;fox&#8221;:<\/p>\n<pre>\/\/*[@id=\"fox\"]\/a<\/pre>\n<p>&nbsp;<\/p>\n<h2>Button Text<\/h2>\n<p>There are two ways to declare a standard button in HTML, discounting the many ways to make something that <em>looks<\/em> like a button, but is not. To determine how an element is declared in the HTML, see <a href=\"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/?page_id=4625\">how to inspect an element in the browser<\/a>.<\/p>\n<p>If the button is declared with the &lt;button&gt; tag and the button says &#8220;press me&#8221;, try this:<\/p>\n<pre>\/\/button(contains(., 'press me')]<\/pre>\n<p>If the button is a form submit button (declared with the &lt;input&gt; tag and type=&#8221;submit&#8221; or =&#8221;button&#8221;) and says &#8220;press me&#8221;, try this:<\/p>\n<pre>\/\/input[@value='press me']<\/pre>\n<p>&nbsp;<\/p>\n<h2>Text of element<\/h2>\n<p>Sometimes a bit of text is styled as a link or button. To find it, try this:<\/p>\n<pre>\/\/*[text()='the visible text']<\/pre>\n<p>&nbsp;<\/p>\n<h2>The Nth element<\/h2>\n<p>To find the Nth element, you must surround your XPath in ()s and then specify the Nth using [n], like this:<\/p>\n<pre>(xpath)[n]<\/pre>\n<p>A very simple example &#8211; find the 3rd <em>link<\/em> on a page:<\/p>\n<pre>(\/\/a)[3]<\/pre>\n<p>To find the 4rd <em>text input<\/em> field on a page:<\/p>\n<pre>(\/\/input[@type=\"text\"])[4]<\/pre>\n<p>You can also traverse from the indexed element. So, to find the <em>link<\/em> in the 2nd <em>div<\/em> with <em>class<\/em> &#8216;abc&#8217;:<\/p>\n<pre>(\/\/div[@class='abc'])[2]\/a<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Web Performance Consulting<\/p>\n<p>        Our experts find out how many users your website can handle!<\/p>\n<p>        &nbsp;<br \/>\n        <a href=\"\/load-testing-consulting-services\/\">Learn More<\/a><\/p>\n<p>If you are new to XPath, the power and flexibility of XPath can be a bit bewildering. This page contains some examples to get you started. After that, I recommend the XPath resources linked on our <a href=\"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/?page_id=4623\">Types of Element &hellip; <a href=\"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/articles\/real-browser-manual\/building-a-testcase\/how-locate-element-the-page\/xpath-locator-examples\/\">Continue reading &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":4616,"menu_order":7,"comment_status":"open","ping_status":"open","template":"manual-page.php","meta":{"footnotes":""},"class_list":["post-4619","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/wp-json\/wp\/v2\/pages\/4619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/wp-json\/wp\/v2\/comments?post=4619"}],"version-history":[{"count":16,"href":"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/wp-json\/wp\/v2\/pages\/4619\/revisions"}],"predecessor-version":[{"id":6053,"href":"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/wp-json\/wp\/v2\/pages\/4619\/revisions\/6053"}],"up":[{"embeddable":true,"href":"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/wp-json\/wp\/v2\/pages\/4616"}],"wp:attachment":[{"href":"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/wp-json\/wp\/v2\/media?parent=4619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}