Locate Elements by Link Text & Partial Link Text in Selenium Webdriver
Locate Elements by Link Text & Partial Link Text in Selenium Webdriver
(https://www.guru99.com/)
Links can be accessed using an exact or partial match of their link text. The examples below
provide scenarios where multiple matches would exist and would explain how WebDriver
would deal with them.
In this tutorial, we will learn the available methods to find and access the Links using
Webdriver. Also, we will discuss some of the common problems faced while accessing Links
and will further discuss on how to resolve them.
(/images/image002.png)
https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 1/11
12/13/2018 Locate Elements by Link Text & Partial Link Text in Selenium Webdriver
(/images/image003(1).png)
When you try to run the WebDriver code below, you will be accessing the first "click here"
link
(/images/image004(1).png)
Code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
driver.get(baseUrl);
driver.findElement(By.linkText("click here")).click();
driver.quit();
}
https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 2/11
12/13/2018 Locate Elements by Link Text & Partial Link Text in Selenium Webdriver
(/images/2-
2017/080817_0703_AccessingLi4.png)
(/images/image005(1).png)
(/images/image006(1).png)
(/images/image007(1).png)
When you execute the WebDriver code below, you will still be taken to Google.
https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 3/11
12/13/2018 Locate Elements by Link Text & Partial Link Text in Selenium Webdriver
(/images/image008(1).png)
Code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class P1 {
System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
driver.get(baseUrl);
driver.findElement(By.partialLinkText("here")).click();
driver.quit();
}
}
(/images/image009(1).png)
How to get Mul ple links with the same Link Text
So, how to get around the above problem? In cases where there are multiple links with the
same link text, and we want to access the links other than the first one, how do we go about
it?
https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 4/11
Most commonly used is By.xpath().
12/13/2018 It is the
Locate Elements most
by Link reliable
Text & Partial Linkone
Text inbut it looks
Selenium complex and non-
Webdriver
readable too.
(/images/2-
2017/080817_0703_AccessingLi10.png)
The parameters for By.linkText() and By.partialLinkText() are both case-sensitive, meaning
that capitalization matters. For example, in Mercury Tours' homepage, there are two links
that contain the text "egis" - one is the "REGISTER" link found at the top menu, and the
other is the "Register here" link found at the lower right portion of the page.
(/images/image010(1).png)
Though both links contain the character sequence "egis," one is the "By.partialLinkText()"
method will access these two links separately depending on the capitalization of the
characters. See the sample code below.
https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 5/11
12/13/2018 Locate Elements by Link Text & Partial Link Text in Selenium Webdriver
(/images/image011(1).png)
Code
System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
driver.get(baseUrl);
String theLinkText = driver.findElement(By
.partialLinkText("egis"))
.getText();
System.out.println(theLinkText);
theLinkText = driver.findElement(By
.partialLinkText("EGIS"))
.getText();
System.out.println(theLinkText);
driver.quit();
https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 6/11
12/13/2018 Locate Elements by Link Text & Partial Link Text in Selenium Webdriver
(/images/image014(1).png)
(/images/image015(1).png)
The WebDriver code below accesses both of these links using By.partialLinkText() method.
(/images/image016(1).png)
Code:
https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 7/11
12/13/2018 Locate Elements by Link Text & Partial Link Text in Selenium Webdriver
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
driver.get(baseUrl);
driver.findElement(By.partialLinkText("Inside")).click();
System.out.println(driver.getTitle());
driver.navigate().back();
driver.findElement(By.partialLinkText("Outside")).click();
System.out.println(driver.getTitle());
driver.quit();
}
}
The output above confirms that both links were accessed successfully because their
respective page titles were retrieved correctly.
Summary
https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 8/11
Prev (/select-option-dropdown-selenium-webdriver.html)
12/13/2018 Locate Elements by Link Text & Partial Link Text in Selenium Webdriver
Report a Bug
Next (/keyboard-mouse-events-files-webdriver.html)
Selenium Tutorials
1) Introduction (/introduction-to-selenium.html)
5) Locators (/locators-in-selenium-ide.html)
6) Enhancements (/enhancing-selenium-ide-script.html)
(https://www.facebook.com/guru99com/)
(https://twitter.com/guru99com)
(https://www.youtube.com/channel/UC19i1XD6k88KqHlET8atqFQ)
(https://forms.aweber.com/form/46/724807646.htm)
https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 10/11
12/13/2018 Locate Elements by Link Text & Partial Link Text in Selenium Webdriver
About
About US (/about-us.html)
Advertise with Us (/advertise-us.html)
Write For Us (/become-an-instructor.html)
Contact US (/contact-us.html)
Career Sugges on
SAP Career Suggestion Tool (/best-sap-module.html)
Software Testing as a Career (/software-testing-career-
complete-guide.html)
Certificates (/certificate-it-professional.html)
Interes ng
Books to Read! (/books.html)
Suggest a Tutorial
Blog (/blog/)
Quiz (/tests.html)
Review (/best-ergonomic-mouse.html)
Execute online
Execute Java Online (/try-java-editor.html)
Execute Javascript (/execute-javascript-online.html)
Execute HTML (/execute-html-online.html)
Execute Python (/execute-python-online.html)
https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 11/11