TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    Selenium

    Find Link using By LinkText in Selenium

    Overview

    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();

     

    By LinkText Find Link

    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.

    By Link Text Example Exercise

    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/

    Official Website:

    https://www.selenium.dev/

    Related Posts

    Windows 10 Settings

    Selenium /

    Add Microsoft Webdriver on Windows OS

    Download Selenium Components

    Selenium /

    Selenium Webdriver

    LambdaTest Testing Cloud SaaS Platform

    Selenium /

    LambdaTest – Testing Cloud SaaS Platform

    Selenium /

    Selenium 3.0 and Mozilla GeckoDriver

    SauceLabs Website

    Selenium /

    Run an Example Test on SauceLabs

    ‹ Locate elements By Tagname in Selenium Webdriver› Find Links using By Partial Link Text in Selenium

    Recent Posts

    • Running Tests in Parallel with Selenium Grid
    • Advanced Selenium Features
    • Locating Web Elements
    • Running the Test Script
    • Writing Your First Selenium Test Script
    • Getting Started with Selenium Automation Testing
    • Setting Up the Environment
    • How can you monitor the Quality Assurance Audit?
    • Leveraging LambdaTest with Appium 2.0
    • Appium 2.0 Plugins ListAppium 2.0 Plugins
    • Touch Actions and Multi-Touch Actions
    • Changes in Drivers and Classes
    • Appium Inspector
    • Capabilities in Appium 2.0
    • Appium 2.0 Driver ListAppium 2.0 Driver Installation & Management
    CyberLink Multimedia Software

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com