{"id":3093,"date":"2020-05-04T19:25:06","date_gmt":"2020-05-04T13:55:06","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3093"},"modified":"2025-04-03T07:06:09","modified_gmt":"2025-04-03T11:06:09","slug":"locating-elements-using-link-text-and-partial-link-text-in-selenium-webdriver","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/locating-elements-using-link-text-and-partial-link-text-in-selenium-webdriver\/","title":{"rendered":"Locating Elements using Link Text and Partial Link Text in Selenium WebDriver"},"content":{"rendered":"\n<p>In <strong><a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\" data-type=\"link\" data-id=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\">Selenium testing<\/a><\/strong>, links can be accessed by using an exact text match or partial text match of their link text. In this article, we will explore the available methods to find and access the links using <strong>Selenium WebDriver<\/strong>, which are essential for automating interactions with web pages. By mastering these techniques, you can make your Selenium testing more efficient and effective when it comes to locating and interacting with links on a webpage.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Accessing links using By.linkText()<\/strong><\/h3>\n\n\n\n<p>In Selenium Webdriver we can access the exact link text by using the method By.linkText(). If we have the same link text for two links, this By.linkText() method will only access the first one. Consider the below HTML code<\/p>\n\n\n\n<p><img fetchpriority=\"high\" decoding=\"async\" width=\"602\" height=\"287\" alt=\"html.png\" src=\"https:\/\/lh6.googleusercontent.com\/uo-mq2F2bBUfThWiUzckIQqOJm7NW4C5yJXhChudvOj3qtvhNq0pfaUN8CsGLjgfh20qB9AuNnDSkVWgvYraBTH-3ISCMVx0Q1duMfqFmmPmgGkcgE_R6luOtzUEXC8hL_Semlqh7_Izd_qhCw\" title=\"\"><\/p>\n\n\n\n<p><em>Save the above file with .html and open the file with Google Chrome<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/EBR-MClSDHp0UG71poEMwI1T5NctqGiHQ3u80RnRCYPuVIV0c6Myx6K67ax9UJ32ybULtqHnKSMfbzqbi5BfmO4ws6VYIg4jkIuCmxYavbiQw9PMJJ6nstFYBJccwCZPgeyiAF1cbBXKFPapng\" alt=\"open.PNG\" title=\"\"><\/figure>\n\n\n\n<p>It will open as<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/MjvxVKcwPDuhs58R1nLROKgy9bmrmeb60J5ek1-bOapiNngLOyqE9wJnkO86hVIN-lkzqhsZ3rcxedYxKUkE_qvAwsH8l57fbevSkM4rwc3tuWW-AohKoIuXIdhaOewyxtHhb7yQv5n2cSCnMQ\" alt=\"click.PNG\" title=\"\"><\/figure>\n\n\n\n<p>When you run the below WebDriver code, you will access the first \u201cclick here\u201d link<\/p>\n\n\n\n<p><img decoding=\"async\" width=\"602\" height=\"230\" alt=\"code.PNG\" src=\"https:\/\/lh4.googleusercontent.com\/o-t4xigrs-fB5TRsJ-tobj3LHMltDXd34Q-r5wvw664lofTNwTzH5uJAhwHuV68wD701AynYbdP2yoXO00K3Z8KPF4aw3wtZRvbmBJGYNyAFbT2U9BdYK-amXMVR6xdkWgtsccv0MF0DonZMyA\" title=\"\"><\/p>\n\n\n\n<p>Let&#8217;s create a test case in which we will automate the following scenarios to handle linkText:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Invoke a Google chrome browser.<\/li>\n\n\n\n<li>Open URL: https:\/\/www.facebook.com\/<\/li>\n\n\n\n<li>Click on the Forgotten account? link.<\/li>\n\n\n\n<li>Close the browser.<\/li>\n<\/ul>\n\n\n\n<p>Now, we will create a test case step by step to understand linkText in WebDriver.<\/p>\n\n\n\n<p><strong>Step 1:<\/strong> Launch Eclipse IDE.<\/p>\n\n\n\n<p><strong>Step 2:<\/strong> Go to File &gt; New &gt; Click on Java Project.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/WQwQR9rLwK_6Ickcn8NXEES98k9BRR4SW9mUeisH1vrhLYd_r4G7m_1R11yT3Sa055jeE11rffemXvxClGYDImxuXzf_f809zM6vnxq-0DpdjCWgWpPXNM1P4hxfdWTUM_L3PrS9o8G66efQJw\" alt=\"File.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 3:<\/strong> Right-click on the src folder and click on the New &gt; class.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/f0TqorWEdBLNdLRekfYxTFaWwaHgqCNuqSKT5tQcOjBllKRnvxFy1WK0IE0Ot50daWPxJvFvsNNfY4ma8vUvsGUCrmNscN9y7ysQhjtzylGlDFpdD4eHXeRwk7CvqE-Qi4JYroSzdCfzKmLy0A\" alt=\"link.PNG\" title=\"\"><\/figure>\n\n\n\n<p>Give your Class name as &#8220;Test_LinkText&#8221; and Select the checkbox \u201cpublic static void main(String[] args) and click on the &#8220;Finish&#8221; button.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/50JJGTRjV8UYyuImGA8Fp88hbhIrQxtidgW5hGKLuv3RDUqf0EsZP2pK_KhG-6mzqnYQjyU_vkvloh74gdLR2Jcn7ZSujTnKFi4o9-R35N1P9yezwx_vulu8S9bBVdVsjlgIsUhzscGk7bLVlw\" alt=\"name.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 4:<\/strong> Invoke the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Google_Chrome\" rel=\"nofollow noopener\" target=\"_blank\">Google Chrome<\/a> browser and set the system property to the path of your chromedriver.exe file.<\/p>\n\n\n\n<p>Here is the sample code to set Chrome driver system property:<\/p>\n\n\n<p><span style=\"font-weight: 400;\">[box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]\/\/ System Property for Chrome Driver\u00a0\u00a0\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0System.setProperty(<\/span><span style=\"font-weight: 400;\">&#8220;webdriver.chrome.driver&#8221;<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">\u201c<\/span><span style=\"font-weight: 400;\"> D:\\\\Drivers\\\\geckodriver.exe<\/span><span style=\"font-weight: 400;\"> &#8220;<\/span><span style=\"font-weight: 400;\">);\u00a0 [\/box]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">After that, we have to initialize the Chrome driver using ChromeDriver Class.<\/span> <span style=\"font-weight: 400;\">Below is the sample code to initialize Chrome driver using ChromeDriver class.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">[box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]\/\/ Instantiate a ChromeDriver class.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">WebDriver driver=new ChromeDriver();\u00a0 [\/box]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We will get the below code to launch the Google Chrome browser after combining both of the above codes.<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> [box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]System.setProperty(<\/span><span style=\"font-weight: 400;\">&#8220;webdriver.chrome.driver&#8221;<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">\u201c<\/span><span style=\"font-weight: 400;\"> D:\\\\Drivers\\\\geckodriver.exe<\/span><span style=\"font-weight: 400;\"> &#8220;<\/span><span style=\"font-weight: 400;\">);\u00a0\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">WebDriver driver=new ChromeDriver();\u00a0 [\/box]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">After that, we need to navigate to the desired URL.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Below is the sample code to navigate to the desired URL:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">[box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]\/\/ Launch Website\u00a0\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">driver.navigate().to(&#8220;<\/span><span style=\"font-weight: 400;\">https:\/\/www.facebook.com\/<\/span><span style=\"font-weight: 400;\">&#8220;);[\/box] <\/span><\/p>","protected":false},"excerpt":{"rendered":"<p>In Selenium testing, links can be accessed by using an exact text match or partial text match of their link text. In this article, we will explore the available methods to find and access the links using Selenium WebDriver, which are essential for automating interactions with web pages. By mastering these techniques, you can make [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":3104,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[693,692],"class_list":["post-3093","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials","tag-link-text-and-partial-link-text","tag-locating-elements"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3093","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3093"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3093\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3104"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}