Steps for Locating Elements Using Partial Link Text in Java

Step 1: Create a Project

Firstly, create a new maven project in your IDE, and for language options choose Java-17. We will use the IntelliJ Idea Community Edition IDE for this tutorial. Alternatively, you could use other IDEs like NetBeans or Eclipse that are free.

The steps shown here are sufficient for all IDE’s:

  1. Maven is a build tool that fetches external libraries that are needed by the Java Runtime Environment.
  2. It adds these to the classpath from where it can be accessed by the JRE.

For more on Maven please read this.

Open your IDE and navigate to File > New > Project. This looks like the following:

Creating the New Project for Locating Elements Using Partial Link Text

Step 2: Set Up the Project Configuration

Next, we will look at the configuration settings for our project.

  1. When you click the Project button from the previous step, a new dialogue box will open.
  2. In this, choose a name for your project and the build type as Maven.

The language should be Java-17. This looks like as follows:

Set Up the Project Configuration for Locating Strategies By Partial Link Text

Step 3: Add the Selenium Dependency

Now add the following selenium dependency in the pom.xml file:

<dependency>

<groupId>org.seleniumhq.selenium</groupId>

<artifactId>selenium-java</artifactId>

<version>4.18.1</version>

</dependency>

Now, your pom.xml file should match the following:

pom.xml file of Locating Strategies By Partial Link Text

Step 4: Building the main class

Now we will develop the main class. We will create a simple automation in this guide. The task we will automate contains steps:

  1. Open the Browser
  2. Locate the link we want to visit using the link text
  3. Navigate to the link
  4. Close the Browser

Without any human interactions. Modify the code in your main class as follows:

Java
public class Main {
    public static void main(String[] args) {
      
          //Use the web driver of your pre-installed browser
        WebDriver driver=new ChromeDriver();
      
          //use the link you want to open
        driver.get("https://www.w3wiki.org/");
      
          //Locate element using partial link text
        WebElement partialLinkElement= driver.findElement(By.linkText("DSA"));
      
          //Click on the link
        partialLinkElement.click();
      
          //Close the browser
        driver.quit();
    }
}

Step 5: Testing the Automation

Now, we will test the automation we have just created. Upon running the code, we get the output of this is as:

Step 6: Error Handling

Now, what happens if try to look for a partial link that does not exist in our code? We see an ugly error on the console and the code behaves weirdly. The error looks like as follows:

Error Handling for Locating Strategies By Partial Link Text

To avoid such situations, we can enclose our code in a try-catch block for a cleaner execution.

Modify the code above as:

Java
public class Main {
    public static void main(String[] args) {
        WebDriver driver=new ChromeDriver();

        //use the link you want to open
        driver.get("https://www.w3wiki.org/");

        try {
            //Locate element using partial link text
            WebElement partialLinkElement= driver.findElement(By.linkText("I am Hello World!"));
          
            //Click on the link
            partialLinkElement.click();
        }
        catch (Exception e) {
            System.out.println("The link was not found.");
        }

        //Close the browser
        driver.quit();
    }
}

Now when you try to run the code you see a simple message that tells you the source of your problem and does not cause the application to behave weirdly. The message logged on the console is as follows:

Output of Error Handling with Try and catch block

Locating Strategies By Partial Link Text Using Java

Web application testing must be rigorous and thorough. For this reason, many tests for web applications are automated. Selenium is an open-source framework that allows us to automate web browser testing. In the following sections, we will use partial link text to locate elements on our HTML code using Java and Selenium. We will further see an example of how we can navigate to other web pages using automation.

Table of Content

  • Steps for Locating Elements Using Partial Link Text in Java
  • Locating Strategies with Partial Link Text on Various Browsers
  • Conclusion
  • Frequently Asked Questions on Locating Strategies By Partial Link Text Using Java

Locating elements using partial link text means we find the elements in our code that point to other web pages and are embedded in some text. This is different from link text because, in link text, you must know the exact text that you are looking for. The partial link text locator is used when you want to locate a piece or some anchor element whose text you don’t completely know.

If the partial link text finds more than one anchor tag containing the text being searched, it considers the first one only.

Similar Reads

Steps for Locating Elements Using Partial Link Text in Java

Step 1: Create a Project...

Locating Strategies with Partial Link Text on Various Browsers

Selenium Web Driver currently provides support for Google Chrome, Internet Explorer, Safari, Microsoft Edge, and Firefox Browser. The dependency we have used in this tutorial is sufficient for all of these drivers. If you wish to have only browser-specific libraries in your application, you could add the specific libraries for each of these drivers:...

Conclusion

In this article, we learned Web application testing will be easily automated using Selenium, which is an open-source framework. In this tutorial, we studied how to locate elements using partial link text using Java and navigate web pages without the help of humans. Using the outlined steps and adding necessary dependencies easily across various browsers....

Frequently Asked Questions on Locating Strategies By Partial Link Text Using Java

How to get Multiple links with the same Link Text?...

Contact Us