Find Link using By LinkText in Selenium
Find Link using By LinkText
In this tutorial, we will learn to find link using By.linkText() method in Selenium Webdriver automation test scripts.
The links on a webpage are represented using anchor tags. Sample a href anchor tag is shown below:
<a href=”Navigation URL” id=”linkID”>TestingDocs</a>
The link text in the example is “TestingDocs” and we can use the By.linkText() method to identify the web element as shown below:
WebElement link = driver.findElement(By.linktext(“TestingDocs”));
Sample Code Example
In this example, we will use the TestLink Login page. The Login page Lost password link which when clicked navigates to lost password page URL. User can retrieve lost password using the page.
The html code for the link is:
<a href=”lostPassword.php?viewer=new” id=”tl_lost_password”>
Lost Password?</a>
To find the link we can use the link text as: Lost Password?
and to click the link we can use the following code in the test:
driver.findElement(By.linkText(“Lost Password?”)).click();
Environment and Tools
The environment and the tools used in the code example are as follows:
- JDK
- Maven build tool
- Selenium Webdriver
- Eclipse IDE
- Firefox Web browser Tools
- Windows 10
Code
Sample test to click the link and print the result to check the link URL.
package com.testingdocs.tests; //Selenium Tutorials - www.TestingDocs.com import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.openqa.selenium.By; import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.TimeUnit; public class FindLinkTextExample { public WebDriver driver; @BeforeClass public void setUp() throws MalformedURLException { DesiredCapabilities dCaps = new DesiredCapabilities(); dCaps.setBrowserName("chrome"); driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), dCaps); } @Test public void testLinkLocator() throws InterruptedException { driver.navigate() .to("http://localhost/testlink/login.php"); driver.manage().window().maximize(); try { driver .findElement(By.linkText("Lost Password?")).click(); driver.manage() .timeouts().implicitlyWait(60, TimeUnit.SECONDS); System.out.println(driver .getCurrentUrl().contains("lostPassword")); } catch (Exception e) { System.out.println(e.getMessage()); } } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
Save the test code and Run the Test from the IDE.
Run as >> Test NG Test.
Exercise
As you can see that, we are checking the Link Navigation URL contains some text phrase. In case, if the text phrase is not present during the test run the test prints false. To simulate we have injected a fault in the test code. The test is marked as Pass even if the behavior is not intended.
The test has no capability to mark as Pass/Fail. Enhance the test code to mark it as Pass/Fail.
—
Selenium Tutorials on this website:
https://www.testingdocs.com/selenium-webdriver-tutorial/