{"id":195,"date":"2017-12-19T11:38:12","date_gmt":"2017-12-19T11:38:12","guid":{"rendered":"http:\/\/loadfocus.com\/blog\/tech\/?p=195"},"modified":"2019-05-09T13:20:20","modified_gmt":"2019-05-09T13:20:20","slug":"how-to-click-a-link-by-text-in-selenium-webdriver-java","status":"publish","type":"post","link":"https:\/\/loadfocus.com\/blog\/tech\/2017\/12\/how-to-click-a-link-by-text-in-selenium-webdriver-java","title":{"rendered":"How to Click a Link by Text in Selenium WebDriver Java"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> &lt; 1<\/span> <span class=\"rt-label rt-postfix\">minute read<\/span><\/span><p class=\"lead\">Here is how to click a link by text with <strong>Selenium WebDriver in Java<\/strong> using the built in <strong>WebDriver<\/strong> helper methods or by XPath:<\/p>\n<h3>Click link by full text using Selenium WebDriver<\/h3>\n<pre class=\"lang-java\"><code class=\"lang-java\">\nWebElement linkByText = driver.findElement(By.linkText(\"My Link\"));\nlinkByText.click();\n<\/code><\/pre>\n           \r\n            <div class=\"onp-locker-call\"  data-lock-id=\"onpLock828911\">\r\n                <p><\/p>\n<h3>Click link by partial text using Selenium WebDriver<\/h3>\n<pre class=\"lang-java\"><code class=\"lang-java\">\nWebElement linkByPartialText = driver.findElement(By.partialLinkText(\"First\"))\nlinkByPartialText.click();\n<\/code><\/pre>\n<h3>Click link by text using XPath in Selenium WebDriver<\/h3>\n<pre class=\"lang-java\"><code class=\"lang-java\">\nWebElement linkByTextUsingXPath = driver.findElement(By.xpath(\"\/\/a[text()='First']\"));\nlinkByTextUsingXPath.click();\n<\/code><\/pre>\n<h3>Click link by partial text using XPath in Selenium WebDriver<\/h3>\n<pre class=\"lang-java\"><code class=\"lang-java\">\nWebElement linkByPartialTextUsingXPath = driver.findElement(By.xpath(\"\/\/a[contains(text(),'ABC')]\"));\nlinkByPartialTextUsingXPath.click();\n<\/code><\/pre>\n<p>More details here on <a href=\"https:\/\/loadfocus.com\/blog\/2013\/09\/05\/how-to-locate-web-elements-with-selenium-webdriver\/\">how to locate elements with Selenium WebDriver<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/loadfocus.com\/\">LoadFocus<\/a>&nbsp;is a cloud testing platform for:<\/p>\n<ul>\n<li><a href=\"https:\/\/loadfocus.com\/load-testing\">Load Testing<\/a><\/li>\n<li><a href=\"https:\/\/loadfocus.com\/website-speed-testing\">Website Speed Testing<\/a><\/li>\n<li><a href=\"https:\/\/loadfocus.com\/mobile-emulation\">Mobile Testing<\/a><\/li>\n<li><a href=\"https:\/\/loadfocus.com\/automated-website-testing\">Automated Website Testing<\/a><\/li>\n<li><a href=\"https:\/\/loadfocus.com\/visual-regression-testing\">Visual Regression Testing<\/a><\/li>\n<\/ul>\n<p><\/p>\r\n            <\/div>\r\n         \r\n\r\n        \n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> &lt; 1<\/span> <span class=\"rt-label rt-postfix\">minute read<\/span><\/span>Here is how to click a link by text with Selenium WebDriver in Java using the built in WebDriver helper methods or by XPath: Click link by full text using Selenium WebDriver WebElement linkByText = driver.findElement(By.linkText(&#8220;My Link&#8221;)); linkByText.click();<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22],"tags":[34,33],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/195"}],"collection":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/comments?post=195"}],"version-history":[{"count":10,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/195\/revisions"}],"predecessor-version":[{"id":336,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/195\/revisions\/336"}],"wp:attachment":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/media?parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/categories?post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/tags?post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}