{"id":2583,"date":"2023-10-30T23:08:00","date_gmt":"2023-10-30T23:08:00","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2023\/10\/30\/locate-webelements-using-linktext-and-partiallinktext-methods-in-selenium\/"},"modified":"2023-10-30T23:08:00","modified_gmt":"2023-10-30T23:08:00","slug":"locate-webelements-using-linktext-and-partiallinktext-methods-in-selenium","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2023\/10\/30\/locate-webelements-using-linktext-and-partiallinktext-methods-in-selenium\/","title":{"rendered":"Locate WebElements using LinkText and PartialLinkText methods in selenium"},"content":{"rendered":"<p><html><body><\/p>\n<section class=\"tcb-post-content tcb-shortcode thrv_wrapper\" data-css=\"tve-u-16ec5d248bb\">\n<p><strong>What you will Learn in this blog:<\/strong><\/p>\n<div class=\"code-block code-block-2\" style=\"margin: 8px auto; text-align: center; display: block; clear: both;\">\n<p><!-- horizontal responsive 2 --><br \/>\n<ins class=\"adsbygoogle\" data-ad-client=\"ca-pub-3231318496783163\" data-ad-format=\"auto\" data-ad-slot=\"2788468763\" data-full-width-responsive=\"true\" style=\"display:block\"><\/ins>\n<\/div>\n<ol class=\"wp-block-list\">\n<li>linkText() method usage in selenium<\/li>\n<li>partialLinkText() method usage in selenium<\/li>\n<li>Source code<\/li>\n<\/ol>\n<p><strong><br \/><\/strong><strong>linkText() method usage in selenium<\/strong><\/p>\n<p>Let us now see the usage of \u2018<em>linkText()<\/em>\u2019 method in selenium.<\/p>\n<p>Launch <a href=\"https:\/\/www.bbc.com\/\" rel=\"noopener\" target=\"_blank\">https:\/\/www.bbc.com\/<\/a>\u00a0<\/p>\n<p>There are many links on the home page like <em>News, Sport<\/em> etc as can be seen below<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/N9nX1H_fdYNqwryh4AYDOnx7m3rHkOMWhoO4EpJnw2uXax0mksEO8jLp3vC88pyOmtL-Vg04Gcro8ZxoGrtXLfQhcoxxmjMYy29I9gS0oewgaMzBFVShLOs1ALGQkSYbJXgN6Tu9aZavs4VJjEg5\"\/><\/figure>\n<p>Let us inspect any link.\u00a0<\/p>\n<p>Notice below that the link is represented by tag \u2018<strong>a<\/strong>\u2019<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/gellKomj37pGCSDzqzoWPEC_00s43IYV5wpKPKXYTYMaJHrPUTKxnKEzlco952W1WArRGqamhz9LxbneJ7A2zF8QPXo0L4ZePr7jTtnWfsytqMIXUXPwWDoPZ529HAoqtPHDz-lUwA4pfquzr_TB\"\/><\/figure>\n<p>We can easily click this link by using <strong><em>linkText()<\/em><\/strong> method, see below. This method uses an exact match against the text present on website (in this example, the exact text should be \u2018News\u2019)<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/uxRH0KL_n00AERZ7nBQHHY8vU2X93fvxS4Pf9mjfuRjwq2Q4HW70jvLAyGE_NQcNsu6pehlwdi1JPrCWHLj_Y6G_ASHhQeEroZ1HKHQoui0rViN0OSUjEybtQmBYpd_DJ3adTqQGrltQ9RsMyVhg\"\/><\/figure>\n<p>When you run this script, notice that the \u2018News\u2019 link gets clicked and the \u2018News\u2019 page opens as shown below<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/ugj6pEoLIPevz3UdTc4c0wMLMewdL6pQfXtyOWgbEvAlE2KmvwWHBRIAKOELnS1SK8bvcGnglwChoM4ZQ-LamapZp4tduMkugYfSpdhFSE5_4yog3OJPpCsHVW6NyrTPxUN04romgQFhlqLPhsYV\"\/><\/figure>\n<p>Let us now click \u2018Sport\u2019 link<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/BpyunnTC2X0OaYD_XJRV3kecMrX901HqBpyOFRYOiLSSPP6wmofKaD8mbates7Pbvi7LS-VjMNoZ54sMTcMEJ-B8zWSnB8pQ-NU_sVcVA7kt-BQaq0FlQv8_lDJ6WwtZcw09FfmB_L5jAI9eMvop\"\/><\/figure>\n<p>Save and execute, the sports page comes up<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/hINnNjyu4EflgFhw9ZWlZ2QzXvpPhzScVtke7KM87ZhVw13-rJsRt1cSRU5j5mcjkJfbThOnNZ_UHFnK3mwGBj2tNmaD3rgqMtHp_x9BoAEcVWJYI7yr0QrnGt_hhvHQAWbUmuITMUCrwHCNjHQE\"\/><\/figure>\n<p>Let us now change \u2018Sport\u2019 to \u2018<strong>s<\/strong>port\u2019 and see whether sport link will be clicked or not<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/35eMEkWXXFF80cTuLdyrFJoHWZGh6gYgP6sQauwWr8c-XXIuqQ7eyDU8ofgTyAvZlmHnKk37WiOw-XkJviu4MxgG6Lb8gbNUYWRc9ubncw9dMX-zx4_uhz3Mr3bI8idFbZOQ020OhtAH1YGpDnP9\"\/><\/figure>\n<p>Execute, notice below we get \u2018NoSuchElementException\u2019<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/gpvpFbBw48n3Ef7lL7aeP416Hb2jutg2YGUZjNugb3M1ZDEpLvhjSR9Pl09f8LtfKBYlxYSXcJYEbGJpufWxKuJTFafBrbFz-I1erHxUDmXoJS3sKo_9s3dQIQqpGAzvPES4O4FDF0zspSXxbJBb\"\/><\/figure>\n<p>This means that, the text is case sensitive and hence we should write the exact text that is seen on website.<\/p>\n<p><strong>PartialLinkText() method usage in selenium<\/strong><\/p>\n<p>Launch <a href=\"https:\/\/www.bbc.com\/\" rel=\"noopener\" target=\"_blank\">https:\/\/www.bbc.com\/<\/a>\u00a0<\/p>\n<p>Right click and inspect any news. As an example, the text of the below news is quite huge \u2018<em>Record surge in days over key 1.5C warming limit<\/em>\u2019. In such cases, it is not a good idea to use <em>linkText()<\/em>, because, in that case, we have to write the entire text for the exact match\u00a0\u00a0\u00a0\u00a0\u00a0<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/aiE8h9vqYACsVhkj02PLHwNzlN3jCx7ZyY2s-jr00cGr1lQeq45dF5_M35D44PF_PbPC_5GAJZWgYPGpFa6ARfffr5hcNSGfXrEnEeOIRxE-KRhfb9BsJeU_NlFJ7pGFmU164AFLz8jAt9QdFyLk\"\/><\/figure>\n<p>Instead, we can use \u2018<strong><em>partialLinkText()<\/em><\/strong>\u2019 method. The advantage with this method is that, we can use only a partial text instead of whole text. So if you see the above figure, we can use partial text, for example, \u2018over key\u2019 to find the link and click it<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/WJIXzXv-Zi00nvsR6oObssRtHds8FYLBrg0FH7M_hV__B5YSez-lNnlkQqCw0YyH8mrlDOQUiFz98H0Nvnwm3lQJdk-vvEo1qgZPlWgv87FclVvuuRCVfBw7qtMNuFfdAoLWqoVLidbOTMKwdAdS\"\/><\/figure>\n<p>When you run this script, notice below that the link is clicked and the news page opens<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/8uH1IcLI3jWIfzZiYiNPU-dVQufvXZbs2e6MQDC4KU9hP5n14ItfYQbQeSv3qjZseeDU1V8PEsSYxcGIws96leWmjmel74UqcZj2UsuIH_rmsU9pSQedwZoT_k5C92YaJHMFqQDRJtTKiUxNJk6y\"\/><\/figure>\n<p>Next, if we change \u2018over key\u2019 to \u2018<strong>O<\/strong>ver key\u2019 and run the script, we will get the \u2018NoSuchElementException\u2019 as expected. The text is case-sensitive\u00a0<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/t1CedlsrqBaI6mr46ViR-mJaI1v8bJb_raeHLjzB4_qEyCd_8ejRcXQj8GBz6WQi2ahm0alnt3CauWBYga35YUWO9xg6WXZRwJsYdlFA0bC0gktamAYL5nhmfdbepJlorJ1shiaH5Q-MRRLgnGPq\"\/><\/figure>\n<p>Let us see another example, launch <a href=\"https:\/\/www.udemy.com\/course\/selenium-real-time-examplesinterview-questions\/\" rel=\"noopener\" target=\"_blank\">https:\/\/www.udemy.com\/course\/selenium-real-time-examplesinterview-questions\/<\/a>\u00a0<\/p>\n<p>Inspect the link \u2018Rahul Shetty\u2019<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/drfUUEnu0LFdoZHIgrRRgN_jNiJDhw9hmixlNzsucN7wTYxZimSmhuAGvHDrHarMEOW-bIVzUGx_t-NDiJ3uToDwdzHFCgNN--L1CW3mXXc5dKVH14kUPI3YTMBX59fBc__BlpFf-81GnslR1Kag\"\/><\/figure>\n<p>Let us use the partial text of this link \u2018Shetty\u2019 to click the link<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/aL-FdRVmZPjt9il_vSp0cnwvP44QH93ElT_bw6oJTbZqu_KkH8F45cRbtbESutYMsNpzslpxTsmGOzUGqDhBO-fA00YS4NngCMumAung6-_SJiVyrgYquilNRMFm2XqhyL9LTKX3dkJwv7pjX9Fu\"\/><\/figure>\n<p>Execute the script.<\/p>\n<p>Notice that the link is clicked and the page scrolls down<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/SnyU4nv1IP68EAW5rgXhQ5mXWMvqUceMvsAqya8Gi7QYlMqh0n7mw_nEp-6XpqdjZxbATf0-EWo2u9FsDOHsV6iekzBwxLIRqlM7BJaohMi_x0zCtZojqN6wV5He3wqUK1cei9Pswu0O7rxsjmNh\"\/><\/figure>\n<p>So this is how we can use <em>linkText()<\/em> and <em>partialLinkText()<\/em> methods to click the links on a webpage.<\/p>\n<p><span style=\"text-decoration: underline;\">Source code<\/span><\/p>\n<pre class=\"wp-block-code\"><code>package Intro;\n\nimport org.openqa.selenium.By;\n\nimport org.openqa.selenium.WebDriver;\n\nimport org.openqa.selenium.firefox.FirefoxDriver;\n\nimport io.github.bonigarcia.wdm.WebDriverManager;\n\npublic class SeleniumScript {\n\npublic static void main(String[] args) {\n\nWebDriverManager.firefoxdriver().setup();\n\nWebDriver driver = new FirefoxDriver();\n\n\/\/linkText method\n\ndriver.get(\"https:\/\/www.bbc.com\/\");\n\ndriver.findElement(By.linkText(\"News\")).click();\n\ndriver.navigate().back();\n\ndriver.findElement(By.linkText(\"Sport\")).click();\n\ndriver.navigate().back();\n\n\/\/partialLinkText method\n\ndriver.findElement(By.partialLinkText(\"over key\")).click();\n\ndriver.get(\"https:\/\/www.udemy.com\/course\/selenium-real-time-examplesinterview-questions\/\");\n\ndriver.findElement(By.partialLinkText(\"Shetty\")).click();\n\n}\n\n}<\/code><\/pre>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/KjIPiMpgT6RRNe9XbWt1nus4QJ5vjMJ-NoWe_Jt5YVxIGKeefq4yM2MZ4t9JkymwXawH_Bw0QN9308dS9fltcbjyOch5NKNldMcDktB8mfUcV099sgsnguzux2JgXuL34NTL5DGCFvzJkL2hKHUd\"\/><\/figure>\n<p>Thank you for reading!<\/p>\n<\/section>\n<p><\/body><\/html><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What you will Learn in this blog: linkText() method usage in selenium partialLinkText() method usage in selenium Source code linkText() method usage in selenium Let us now see the usage of \u2018linkText()\u2019 method in selenium. Launch https:\/\/www.bbc.com\/\u00a0 There are many links on the home page like News, Sport etc as can be seen below Let [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2583","post","type-post","status-publish","format-standard","hentry","category-selenium","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2583","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=2583"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2583\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=2583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=2583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=2583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}