{"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\"> 2<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span><p class=\"lead\"><!-- pn-tldr --><\/p>\n<h2>Key takeaways<\/h2>\n<ul>\n<li>linkText matches the full visible text; partialLinkText matches a substring.<\/li>\n<li>Both break on copy changes, so prefer a stable attribute when the text is likely to move.<\/li>\n<li>Whitespace and case matter, which is the usual reason a correct-looking locator fails.<\/li>\n<\/ul>\n<p><!-- \/pn-tldr -->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<p>[emaillocker]<\/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>[\/emaillocker]<!-- pn-faq --><\/p>\n<h2>Frequently Asked Questions<\/h2>\n<h3>What is the difference between linkText and partialLinkText?<\/h3>\n<p>linkText matches the full visible text of the anchor, partialLinkText matches any substring of it. Use the partial form when the label contains a value you cannot predict, such as a name or a count.<\/p>\n<h3>Why does my linkText locator not find the link?<\/h3>\n<p>Usually whitespace or case. The match is on the rendered text exactly as it appears, so a stray non breaking space, a line break in the markup or a CSS text-transform will all defeat a locator that looks correct.<\/p>\n<h3>Is locating by text a good idea?<\/h3>\n<p>It reads well, but it breaks whenever someone edits the copy, and it breaks in every localised build. Where the text is likely to move, prefer a stable id or a test attribute and keep text locators for content you are actually asserting on.<\/p>\n<p><!-- \/pn-faq --><!-- pn-related-reading --><\/p>\n<h2>Related reading<\/h2>\n<ul>\n<li><a href=\"https:\/\/loadfocus.com\/blog\/2013\/09\/how-to-locate-web-elements-with-selenium-webdriver\">How to Locate Web Elements with Selenium WebDriver?<\/a><\/li>\n<li><a href=\"https:\/\/loadfocus.com\/blog\/2016\/10\/find-all-the-links-on-a-webpage-with-selenium-webdriver-in-java\">Find All Links on a Page With Selenium WebDriver<\/a><\/li>\n<li><a href=\"https:\/\/loadfocus.com\/blog\/2016\/10\/how-to-find-by-xpath-text-that-contains-apostrophe-single-quote-in-selenium-webdriver\">XPath Text With an Apostrophe (Single Quote) in Selenium<\/a><\/li>\n<\/ul>\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\"> 2<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span>Key takeaways linkText matches the full visible text; partialLinkText matches a substring. Both break on copy changes, so prefer a stable attribute when the text is likely to move. Whitespace and case matter, which is the usual reason a correct-looking locator fails. Here is how to click a link by text with Selenium WebDriver in&#8230;  <a href=\"https:\/\/loadfocus.com\/blog\/tech\/2017\/12\/how-to-click-a-link-by-text-in-selenium-webdriver-java\" class=\"more-link\" title=\"Read How to Click a Link by Text in Selenium WebDriver Java\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":496,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,44],"tags":[34,33],"class_list":["post-195","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-ui-testing","tag-click-link-by-partial-text-using-webdriver","tag-click-link-by-text-in-selenium-webdriver"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"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:featuredmedia":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/media\/496"}],"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}]}}