{"id":30,"date":"2025-09-06T08:34:23","date_gmt":"2025-09-06T08:34:23","guid":{"rendered":"https:\/\/blog.kerjarapi.com\/?p=30"},"modified":"2025-09-06T08:54:44","modified_gmt":"2025-09-06T08:54:44","slug":"selecting-elements-using-xpath-for-automation-in-katalon-studio","status":"publish","type":"post","link":"https:\/\/blog.kerjarapi.com\/?p=30","title":{"rendered":"Selecting Elements Using XPath for Automation in Katalon Studio"},"content":{"rendered":"\n<p>XPath is used to uniquely identify an element in an XML or HTML documents. I have mentioned briefly on how we can use XPath to select an element and use it as an object in automation. For this blog, I want to cover the use of XPath in web testing in more depth. I just want to share how I use custom XPath to select elements, that can work when dealing with dynamic elements, and can be easily parameterized for more flexible use.<\/p>\n\n\n\n<p>I want to cover some of the ways I use custom XPaths. So I won\u2019t cover all the ways or methods here. But I will share my resource on how I learned it at the end of this blog. I will be using <a href=\"https:\/\/demoqa.com\/buttons\" target=\"_blank\" rel=\"noopener\"><em>this<\/em><\/a> page for our example in this blog.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Basic XPath element selector<\/h2>\n\n\n\n<p>For this example, let\u2019s say that I want to select the \u201cDouble Click Me\u201d button, and this is how the HTML structure is:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"772\" src=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-1024x772.png\" alt=\"\" class=\"wp-image-31\" srcset=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-1024x772.png 1024w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-300x226.png 300w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-768x579.png 768w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image.png 1152w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>By clicking right and selecting \u201cInspect\u201d we can see the element is a button and has attributes id, type, class, and text \u201cDouble Click Me\u201d. We can create a unique XPath selector to select this element. for example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/button&#91;@id='doubleClickBtn']\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"667\" height=\"508\" src=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-1.png\" alt=\"\" class=\"wp-image-32\" srcset=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-1.png 667w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-1-300x228.png 300w\" sizes=\"auto, (max-width: 667px) 100vw, 667px\" \/><\/figure>\n\n\n\n<p>This will successfully select the button element we want, because it is the only button that has the id \u201cdoubleClickBtn\u201d<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using text(), contains(), and starts-with()<\/h2>\n\n\n\n<p>This time, instead of using the \u201cid\u201d attribute, let&#8217;s use the text of the button, \u201cDouble Click Me\u201d. We can do that by using the \u201ctext()\u201d method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/button&#91;text()='Double Click Me']\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"510\" src=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-2.png\" alt=\"\" class=\"wp-image-35\" srcset=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-2.png 666w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-2-300x230.png 300w\" sizes=\"auto, (max-width: 666px) 100vw, 666px\" \/><\/figure>\n\n\n\n<p>This will successfully select the button we want because it\u2019s the only button to have the \u201cDouble Click Me\u201d text.<\/p>\n\n\n\n<p>Next, let\u2019s try the contains() method. This method lets you select an attribute without typing the whole attribute name or text. For example, to select the same button as last time, we can try:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/button&#91;contains(text(), 'Double')]\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"661\" height=\"512\" src=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-3.png\" alt=\"\" class=\"wp-image-34\" srcset=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-3.png 661w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-3-300x232.png 300w\" sizes=\"auto, (max-width: 661px) 100vw, 661px\" \/><\/figure>\n\n\n\n<p>This time, we only use the button text partially. It will look for a button that contains the text \u201cDouble\u201d and select the element.<\/p>\n\n\n\n<p>We can also use the starts-with() method, and as the name implies, it can help us select an element by only stating the start of the attribute or text, for example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/button&#91;starts-with(text(), 'Double')]\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"512\" src=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-4.png\" alt=\"\" class=\"wp-image-36\" srcset=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-4.png 666w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-4-300x231.png 300w\" sizes=\"auto, (max-width: 666px) 100vw, 666px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using AND &amp; OR statement<\/h2>\n\n\n\n<p>We can use one or more attributes and texts when selecting. To help us select the element, we can use the AND &amp; OR statements. To select our beloved \u201cDouble Click Me\u201d button, we can try:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/button&#91;@type='button' and text()='Double Click Me']\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"665\" height=\"507\" src=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-5.png\" alt=\"\" class=\"wp-image-37\" srcset=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-5.png 665w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-5-300x229.png 300w\" sizes=\"auto, (max-width: 665px) 100vw, 665px\" \/><\/figure>\n\n\n\n<p>Using the button attribute type \u201cbutton\u201d and the button\u2019s text \u201cDouble Click Me\u201d, we have successfully selected our button again.<\/p>\n\n\n\n<p>We can also use OR statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/button&#91;@type='button' or text()='Double Click Me']\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"660\" height=\"512\" src=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-6.png\" alt=\"\" class=\"wp-image-38\" srcset=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-6.png 660w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-6-300x233.png 300w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/figure>\n\n\n\n<p>But when using the OR statement, we can\u2019t uniquely find our \u201cDouble Click Me\u201d button. It also selected three other buttons because they too have the attribute type \u201cbutton\u201d. Following the AND and OR statement logic, if any of the statements [@type=\u2019button\u2019] or [text()=\u2019Double Click Me\u2019] is true, then the element is selected.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the following-sibling method<\/h2>\n\n\n\n<p>Let\u2019s see if we can find our buttons using the following-sibling method. We can see in our screenshot below, that our &lt;h1&gt; is on the same level as our divs (marked in green), and each divs has our buttons (marked in yellow).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"619\" height=\"375\" src=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-7.png\" alt=\"\" class=\"wp-image-39\" srcset=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-7.png 619w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-7-300x182.png 300w\" sizes=\"auto, (max-width: 619px) 100vw, 619px\" \/><\/figure>\n\n\n\n<p>So we can try:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/h1\/\/following-sibling::div\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"656\" height=\"522\" src=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-8.png\" alt=\"\" class=\"wp-image-40\" srcset=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-8.png 656w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-8-300x239.png 300w\" sizes=\"auto, (max-width: 656px) 100vw, 656px\" \/><\/figure>\n\n\n\n<p>And it will select all three divs after the &lt;h1&gt; that are on the same level.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the parent method<\/h2>\n\n\n\n<p>For this example, let\u2019s say we have found our \u201cDouble Click Me\u201d button, but we want to select the div that contains our button instead. We can do that by using this XPath:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/button&#91;text()='Double Click Me']\/\/parent::div\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"667\" height=\"246\" src=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-9.png\" alt=\"\" class=\"wp-image-41\" srcset=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-9.png 667w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-9-300x111.png 300w\" sizes=\"auto, (max-width: 667px) 100vw, 667px\" \/><\/figure>\n\n\n\n<p>It will select the parent div from our button element.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Ancestor method<\/h2>\n\n\n\n<p>When using the parent method, we can only select one parent from our element. But what if we want to select all divs that are above our element. We can use that by using the ancestor method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/button&#91;text()='Double Click Me']\/\/ancestor::div\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"657\" height=\"461\" src=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-10.png\" alt=\"\" class=\"wp-image-42\" srcset=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-10.png 657w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-10-300x211.png 300w\" sizes=\"auto, (max-width: 657px) 100vw, 657px\" \/><\/figure>\n\n\n\n<p>This will select all divs that are above or all the great great parents to our button element.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Selecting SVG element<\/h2>\n\n\n\n<p>To select and SVG element, there needs to be a little more syntax included. For example in <a href=\"https:\/\/demoqa.com\/checkbox\" target=\"_blank\" rel=\"noopener\"><em>this<\/em><\/a> page I want to click on the \u201cHome\u201d checkbox, and we want to select the svg element. We can do that by adding \u201c<strong>\/\/*[local-name()=&#8217;svg&#8217;]<\/strong>\u201d. So to select out checkbox, we can use XPath:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/span&#91;@class='rct-checkbox']\/\/*&#91;local-name()='svg']\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"562\" src=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-11-1024x562.png\" alt=\"\" class=\"wp-image-43\" srcset=\"https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-11-1024x562.png 1024w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-11-300x165.png 300w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-11-768x422.png 768w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-11-1536x843.png 1536w, https:\/\/blog.kerjarapi.com\/wp-content\/uploads\/2025\/09\/image-11.png 1627w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What to do when you can\u2019t uniquely identify the element<\/h2>\n\n\n\n<p>Sometimes we get into the trouble of not finding the unique XPath to identify our element. I have a simple trick to solve this. Let\u2019s go back to our buttons again. When we try:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/button&#91;@class='btn btn-primary']\n<\/code><\/pre>\n\n\n\n<p>It will select all three buttons, because all of them have the same \u201cbtn btn-primary\u201d class. To choose the first button we can use this syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(\/\/button&#91;@class='btn btn-primary'])&#91;1]\n<\/code><\/pre>\n\n\n\n<p>Surely as you can guest, to select the second and third button, you can simply change the<\/p>\n\n\n\n<p>\u201c[1]\u201d into \u201c[2]\u201d and \u201c[3]\u201d. You can try it on <a href=\"https:\/\/demoqa.com\/buttons\" target=\"_blank\" rel=\"noopener\"><em>this<\/em><\/a> page.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(\/\/button&#91;@class='btn btn-primary'])&#91;2]\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>(\/\/button&#91;@class='btn btn-primary'])&#91;3]\n<\/code><\/pre>\n\n\n\n<p>For more resources on XPath, I recommend <a href=\"https:\/\/www.tutorialspoint.com\/xpath\/index.htm\" target=\"_blank\" rel=\"noopener\">this<\/a> page to learn more.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>XPath is used to uniquely identify an element in an XML or HTML documents. I have mentioned briefly on how we can use XPath to select an element and use it as an object in automation. For this blog, I want to cover the use of XPath in web testing in more depth. I just [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,7],"tags":[],"class_list":["post-30","post","type-post","status-publish","format-standard","hentry","category-en","category-katalon-auto"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/blog.kerjarapi.com\/index.php?rest_route=\/wp\/v2\/posts\/30","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kerjarapi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kerjarapi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kerjarapi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kerjarapi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=30"}],"version-history":[{"count":2,"href":"https:\/\/blog.kerjarapi.com\/index.php?rest_route=\/wp\/v2\/posts\/30\/revisions"}],"predecessor-version":[{"id":46,"href":"https:\/\/blog.kerjarapi.com\/index.php?rest_route=\/wp\/v2\/posts\/30\/revisions\/46"}],"wp:attachment":[{"href":"https:\/\/blog.kerjarapi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kerjarapi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kerjarapi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}