{"id":2641,"date":"2023-10-31T16:27:44","date_gmt":"2023-10-31T16:27:44","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=2641"},"modified":"2023-12-03T06:52:29","modified_gmt":"2023-12-03T06:52:29","slug":"identify-elements-using-xpath","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2023\/10\/31\/identify-elements-using-xpath\/","title":{"rendered":"Identify elements using Xpath"},"content":{"rendered":"\n<ol class=\"wp-block-list\">\n<li>Definition of <a href=\"https:\/\/docs.google.com\/document\/d\/1NvkXVPyJYEoLnKzkZoLmwmTKBnZZqJgD\/edit#bookmark=id.1fob9te\" target=\"_blank\" rel=\"noopener\">xpath locator<\/a> in Selenium webdriver&nbsp;<\/li>\n\n\n\n<li>xpath syntax\/example<\/li>\n\n\n\n<li><a href=\"https:\/\/docs.google.com\/document\/d\/1NvkXVPyJYEoLnKzkZoLmwmTKBnZZqJgD\/edit#bookmark=id.3znysh7\" target=\"_blank\" rel=\"noopener\">Simplified xpath syntax<\/a><\/li>\n\n\n\n<li>xpath regular expression to identify element (\u2018contains\u2019 keyword)<\/li>\n\n\n\n<li>Parent-child xpath relation<\/li>\n\n\n\n<li>Difference between relative and absolute xpath<\/li>\n\n\n\n<li>Source code<\/li>\n<\/ol>\n\n\n\n<p><strong>Definition of xpath locator in Selenium webdriver<\/strong><strong><\/strong><br>We can identify elements using their xpath (which is simply an address of an element on the webpage). Xpath help us reaching a desired element on the web page.<\/p>\n\n\n\n<p><strong>xpath syntax\/example<\/strong><\/p>\n\n\n\n<p>The xpath syntax:<strong> <\/strong><strong>\/\/tagname[@attribute=&#8217;value&#8217;]<\/strong><br>xpath example: <strong>\/\/input[@name=&#8217;Submit&#8217;]<\/strong>&nbsp;<\/p>\n\n\n\n<p>In the above example, we are trying to find an element that has the tag name \u201c<em>input<\/em>\u201d &amp; the value of its \u201c<em>name<\/em>\u201d attribute is \u2018<em>Submit<\/em>\u2019.<\/p>\n\n\n\n<p>Let us see a practical example.&nbsp;<\/p>\n\n\n\n<p>Launch <a href=\"https:\/\/www.bing.com\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.bing.com\/<\/a> and inspect the search text field.&nbsp;<\/p>\n\n\n\n<p>Notice below that the search field is represented by tagname \u2018<em>textarea<\/em>\u2019, attribute \u2018<em>id<\/em>\u2019 and the attribute\u2019s value is \u2018<em>sb_form_q<\/em>\u2019<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/6gdhRSO0tky-LS_xc4dRhlJ8V_-tfEN4l-HK2WprGX8Nog8AzakJ-u9XiC7oh1jt9gbwIPwxPKyyVIPuEwD7ACFDxnFbe4js__NhHE3oOoQsIQEYG4Gx-zS12iHZZpoGi8_PYMz4kWeHP2ansGiC\" width=\"602\" height=\"374\"><\/p>\n\n\n\n<p><br>Based on xpath\u2019s syntax, our xpath would become:&nbsp; <strong>\/\/textarea[@id=&#8217;sb_form_q&#8217;]<\/strong> (see line 14)<br><br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/rgi-52T_PDeGaOhTf9L4Y00OPvdFdkNFyONLftTPAbq4VO3G70BbcsdpzyRE-xsxEa71LOuksPHzgmNeK3tL7Z1ctNXif_Zq5UgAIwCf9rCiQtGv0P24cn8WGOEjFwFtG8_E4rrFR2n2ZnY480zk\" width=\"668\" height=\"229\"><br>Let us enter some text in the search field \u201crahulshettyacademy\u201d and run the script. Notice below the search field is successfully identified using the xpath and the desired text gets typed in the search field<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/Y6MFH_XvY89tCRND_oNK1Ej1q0RG4SukhPD1CS-uR8onY7RJjlFpM6MfukR8R91kfRZLVz5_vmoydnGLGFhydo7HPxqSqfBZLcy603jgYCW_AIkVOfgqNgROWnZnRxtWgVwEVrEcV-4wKtERcXHf\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Simplified xpath syntax<\/strong><strong> <\/strong><strong><br><\/strong>We have already seen the xpath syntax:<strong> <\/strong><strong><em>\/\/tagname[@attribute=&#8217;value&#8217;]<\/em><\/strong><\/p>\n\n\n\n<p>We can simplify this syntax by replacing <strong><em>tagname<\/em><\/strong> with star <strong><em>*<\/em><\/strong>&nbsp; (star <strong><em>*<\/em><\/strong> represents <strong><em>any<\/em><\/strong>).<br><br>So the simplified xpath syntax becomes <strong><em>\/\/*[@attribute=&#8217;value&#8217;]<\/em><\/strong><strong><br><\/strong>As an example: <strong><em>\/\/*[@id=&#8217;sb_form_q&#8217;]<\/em><\/strong><em><br><\/em><\/p>\n\n\n\n<p>So the simplified syntax means that:<br>find an element on the webpage (irrespective of <em>any<\/em><em> <\/em>tagname) whose attribute \u2018<em>id<\/em>\u2019 has the value \u2018<em>sb_form_q<\/em>\u2019<\/p>\n\n\n\n<p>Let us comment line 14. In line#15, we have used the simplified xpath syntax<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/p80B0_d_zk-nP0rloN7IlDqBkogg1-fp4XhEGXYLCJrhpIShLLhkxJBqIKYqFssvUVTUtSoEd0y_oy9QRB1H6GQE1HZMbzMZs-cgZwrNE_9kY-hadr2bX70U2li0tcK_ryQvvXE9iFeDkeMQntdO\" width=\"674\" height=\"143\"><br><\/p>\n\n\n\n<p>Save and execute script, the simplified syntax works just fine<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/Y6MFH_XvY89tCRND_oNK1Ej1q0RG4SukhPD1CS-uR8onY7RJjlFpM6MfukR8R91kfRZLVz5_vmoydnGLGFhydo7HPxqSqfBZLcy603jgYCW_AIkVOfgqNgROWnZnRxtWgVwEVrEcV-4wKtERcXHf\" width=\"602\" height=\"303\"><br><\/p>\n\n\n\n<p>Please remember that we cannot use double quotes within double quotes in xpath syntax. This means that, when we write the xpath syntax, we start by opening and closing the braces with double quotes viz <em>By.xpath(<\/em><strong><em>\u201c<\/em><\/strong><em> <\/em><strong><em>\u201c<\/em><\/strong><em>)<\/em><em>.<\/em><em><br><\/em>Since we have already used one set of double quotes, we cannot write another set of double quotes, below will give syntax error<br><em>By.xpath(&#8220;\/\/input[@value=<\/em><strong><em>&#8220;<\/em><\/strong><em>Submit<\/em><strong><em>&#8220;<\/em><\/strong><em>]&#8221;)<\/em><\/p>\n\n\n\n<p><br>To get rid of syntax error, simply convert second set of double quotes with single quote:<br><em>By.xpath(&#8220;\/\/input[@value=<\/em><strong><em>&#8216;<\/em><\/strong><em>Submit<\/em><strong><em>&#8216;<\/em><\/strong><em>]&#8221;)<\/em><\/p>\n\n\n\n<p><strong>xpath regular expression to identify element (\u2018contains\u2019 keyword)<\/strong><\/p>\n\n\n\n<p>We can use xpath regular expressions to identify an element. The regular expressions can be used if the value of an attribute changes dynamically.&nbsp;<\/p>\n\n\n\n<p>As an example, the value of \u2018id\u2019 attribute might be <em>397mai543<\/em> in one session and <em>860mai631<\/em> in another session. As can be seen, the prefix and suffix is dynamically changing. The static content in both of these values is \u2018<em>mai<\/em>\u2019.&nbsp;<\/p>\n\n\n\n<p>The \u2018contains\u2019 keyword can be used to create a regular expression. The syntax would be <strong><em>\/\/tagname[contains(@attribute,\u2019value\u2019)]<\/em><\/strong><br><\/p>\n\n\n\n<p>Inspect \u2018Log In\u2019 button on the facebook page and notice the value of \u2018id\u2019 attribute (<em>u_0_5_<\/em><strong><em>gr<\/em><\/strong>)<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/8oxDWTahWINPUOjvAmIpKUxZvC1rwt6jmls2i0fMbGB5GosrAVUJlpHtmYLa8WJu1b7rNiTUL_AboVHQf6qn88zSYKPVu7xGg14uNfAufTZ4V_uISymjszQnS_QGtczhmAh4bwFvQMAbATEZGzmf\" alt=\"\"\/><\/figure>\n\n\n\n<p>Open another browser session and inspect the \u2018Log In\u2019 button once again<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/_5KeF9cfYtUWG2tmXkxYlE6DY4s5jCF7DUureoDikqd3mvbeZyxQx19UFfmv8FiV8akJIFFTITEjtFV11d4sFjhysuJTAxuvljAt58uIAGhl6TQ-jyqktogRW3t4xcGQm71XVgIb-24L14ms5xgP\" width=\"602\" height=\"203\"><br>Notice above that the value of \u2018id\u2019 attribute changes to (<em>u_0_5_<\/em><strong><em>RR<\/em><\/strong>)<\/p>\n\n\n\n<p>Similarly, the value changes in another session<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/e4is5oIENfBuWqst61UBomJhgjhW3KMTwDEp0Wa0WeYG8deGqMaY7oR8z_OorZX2DVT0AHl5iXtOLqqWjpLUDAOSX9q-eILMjYWWZOnEMC6P1mJEb7fA2HzSg8jZFcxEJjtvkBHuomSUIQxiHAFb\" alt=\"\"\/><\/figure>\n\n\n\n<p>The static part in these three cases is same: <em>u_0_5<\/em><\/p>\n\n\n\n<p>We can use the static value of the \u2018id\u2019 attribute alongwith the \u2018contains\u2019 keyword as shown below<br><br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/1R8GlJiOTJ6uOvBIz62hbH6TtrVjES-HOdLzHXugRwYJxjdInypqMAKeMwLtqJTd47xMfcZqSQrb98l4V8eQdgvUN7xMT1v-wYguFMKRp6_UieShNcfCKdEwZvpJzfhmUvo5awnQuSpdUixoZz0X\" width=\"657\" height=\"64\"><br><br>Run, notice that the \u2018Log In\u2019 button gets clicked<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/TT-auVnBXxCxLvQsir0FAZMcmpW34U88UI9es9M4q3RceGeqzhZugrTGyfNAFytJ5tYu0Ncy5VjmTqnSfnIeh8s263Z9TBqBMPVYuDOD0DIAxLsjhLr11kDfe6qjz5B5kIKFdf27JQmBwYK5YteW\" width=\"602\" height=\"352\"><\/p>\n\n\n\n<p>This is how we can use the \u2018contains\u2019 keyword to form an xpath regular expression.<\/p>\n\n\n\n<p>If we look at another example (google.com) and inspect the search field, we will notice that the value of \u2018<em>data-ved<\/em>\u2019 attribute keeps changing<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/hUbvpPqzzkKrMk3gbppz5F7r3aOoBsP_WhjM9KUTTB2L3JY3eJYO3JaDsSdi2Qe2JLlpqNU5fT8_CMrwgb7kZmJJRg4Vn2EZj9qBeWgNyNPFfIq4yloSZoVau2amUc-NDwctQNMfX3a9JhvDGPBw\" width=\"661\" height=\"393\"><\/p>\n\n\n\n<p>The value contains the static portion <em>CAQ<\/em> and we can make use of it to identify this search field<br><br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/nqgpSUXYOK9XyirCuvyr1H2grmBsqvz0_-xNxpeDNH7yUVvKJ8S0Y7f5hWbv9aRbbxLHpOtpNE_-lwFfiG0IrNaWEJbAlQcHSLrpr76sXCzS6yLbYvge9rK-E-yqg0FSqkwDE90Mpdh12kaiKcj6\" width=\"673\" height=\"80\"><\/p>\n\n\n\n<p><br>Run the script, the search field gets identified and the text gets entered<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/g6HZEvoXEtJOgLJWGUTiWb51Ntimtqmo-hEfR2byFefxEFcpjSDpXh6ELXnqR85HhTIOukqhN2F7HhTJGVRC6RKbGTX-J6O3Sj8P_7gZSxnzSVmW746Y2R2vHVaXB_ihagg1HIFt1nlZUB0L1lgr\" width=\"602\" height=\"228\"><br><strong>Parent-child xpath relation<\/strong><\/p>\n\n\n\n<p>Assume that there is no unique attribute to identify an element on a webpage. Let us assume that the google search field cannot be identified by any unique attribute. In these cases, an xpath is created by using the parent to child relationship. If child does not have a unique attribute, we than look for one of its parent having the unique attribute. We can than write an xpath starting from parent and keep traversing till the child (that has no unique attribute) with the help of tagnames<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/gkElK-WqRf5TRdpKHQli4SqNJnSud9kPVFMCA0HdkVEJQVco2-J6REyxBiI6BmrEH6eqnzWUfmleTNTQRIalkokKQvrRUwoiTfMcH5m20bE-0jYcB2guyZhcPJdjNeViqjAEUC7_nylkIXCk603W\" alt=\"\"\/><\/figure>\n\n\n\n<p>As seen in the diagram below, the parent &lt;div&gt; tag has a unique \u2018class\u2019 attribute. The child tag is &lt;textarea&gt;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/7ZM7yTgXc7Td1x_jpNuz7LQ5GLbbjGPcn-ThJ-0Ejgc-8qnDtFH3O_z7CNlpxKYfqOrkH2xWDAkGK43_4_dnfh4yxJtsXtLld_bO92hYwRBQ0P9JugFRzjHNUKbyxAA7ITKY6iOsFZmJG9Ig2F0F\" alt=\"\"\/><\/figure>\n\n\n\n<p>So the parent &lt;div&gt; tag has a unique attribute. Please note that we can keep going one level up till a parent having unique attribute is found.&nbsp;&nbsp;<\/p>\n\n\n\n<p>So, we have found a parent that has \u2018div\u2019 tag having \u2018class\u2019 value &#8216;a4bIc&#8217;.<strong> <\/strong>The parent can be represented by xpath <em>\/\/div[@class=&#8217;a4bIc&#8217;]<\/em>.&nbsp;<\/p>\n\n\n\n<p>From this parent tag, we will traverse to desired child tag &lt;<em>textarea<\/em>&gt;&nbsp;<\/p>\n\n\n\n<p>The child tag (here) is the actual search field.&nbsp;<\/p>\n\n\n\n<p>The custom xpath from parent to child path becomes <strong><em>\/\/div[@class=&#8217;a4bIc&#8217;]<\/em><\/strong><strong><em>\/textarea<\/em><\/strong>&nbsp;<\/p>\n\n\n\n<p>We can now use this parent-child xpath to enter some value in the text field<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/c-Fw_CmZPRkE9__l6vzkg1eNVu9hEouVnLiottO3aXE_8GmyLtViEPT0ddmyeXsIGHZm8mHit7K3oI5pdyW2MZBW5QoiKSF5j-DQWHldisI2jbFFTaGZqG12YaagpzD5ON_jQ_2dZGE9P_SGyapE\" width=\"630\" height=\"68\"><br>Run, \u2018rahulshettyacademy\u2019 gets typed in search field<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/UcMBcSW9_PWNFhWrCSPwAPyFaVhbdydeinO4_PngiG8SkmNlbQwViJhcm0g8D2RhbjCBK4YgSjKiUpiIWFtB72UP1iPgxBDjYaS6ejeYk5nIL2LYuOtYPdMYmHO5JEgk5Q3yjoXbUaZTMd2_ftoi\" width=\"602\" height=\"245\"><br><\/p>\n\n\n\n<p><strong>Difference between relative and absolute xpath<\/strong><strong> <\/strong><strong><br><\/strong>In the case of relative xpath, we can jump directly to desired web element. We do not need the help of parent node to find the web element. Relative xpath syntax starts with <strong>\/\/<\/strong>.&nbsp;<\/p>\n\n\n\n<p>In the below diagram, the relative xpath <strong>\/\/*[@id=&#8217;sb_form_q&#8217;] <\/strong>can be used to identify the search field. We haven\u2019t taken the help of any parent tag.<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/-DNUdremVCRzkWwuloQE3d_rG-8UtpDWOeeyLoJC97gpnaC6AQCfIn7v6GY7At350sR220QsQMFshYIL0d2_ibi3tMT2vuNLz4u5Q3PKev8Obhz_J4rALv1ZH3_BVkaZwIxKBNyg6FM3vUaP26pw\" width=\"557\" height=\"371\"><br><\/p>\n\n\n\n<p>However, for finding the absolute xpath, we have to take the help of all the parent nodes to reach the desired child element. The absolute xpath syntax starts with single slash \/ and we would be traversing like: \/parent\/child1\/child2\/\u2026..<\/p>\n\n\n\n<p>Inspect the search field of <a href=\"http:\/\/www.bing.com\/\" target=\"_blank\" rel=\"noopener\">www.bing.com<\/a>&nbsp;<\/p>\n\n\n\n<p>Using the selector\u2019s hub, identify the absolute xpath.<\/p>\n\n\n\n<p>Notice that the absolute xpath starts at \/html and ends at \/textarea<\/p>\n\n\n\n<p>\/html[1]\/body[1]\/div[1]\/div[1]\/div[3]\/div[2]\/form[1]\/div[1]\/div[1]\/textarea[1]<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/S12Kd9ylp-IaFv5rmuuERfmx84hhbkAaLhJ0YOWdtxT2KjQTMDCthS6VRPmAMR470N-T4-tIvvbwPltgvN9JhfamZeJDVWQ56QUFRVEOe0P2vx_Vzysi-bOg2I0LKgoI1pfMrlYj8RUo_5AIXXQU\" width=\"652\" height=\"298\"><\/p>\n\n\n\n<p><strong>Source code<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/XUtzLitFxWDD5ZSLBgb9c9zInO_SapmkRE2LYHj_EOxDrhndc27Cca6Xfo4QdwHdtmsDEdRXbcw1dP9Vi68JjQL0UsXLAfN_hcWxZztvBahhZxOG6hoyiOJo5lVoBVZ--fQcEypcnF0j1itf9IyK\" alt=\"\"\/><\/figure>\n\n\n\n<p>Thank you!<\/p>\n\n\n\n<p><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Definition of xpath locator in Selenium webdriverWe can identify elements using their xpath (which is simply an address of an element on the webpage). Xpath help us reaching a desired element on the web page. xpath syntax\/example The xpath syntax: \/\/tagname[@attribute=&#8217;value&#8217;]xpath example: \/\/input[@name=&#8217;Submit&#8217;]&nbsp; In the above example, we are trying to find an element that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1290,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2641","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2641","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=2641"}],"version-history":[{"count":5,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2641\/revisions"}],"predecessor-version":[{"id":2823,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2641\/revisions\/2823"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1290"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=2641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=2641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=2641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}