Open In App

Locating Strategies By Partial Link Text Using Java

Last Updated : 14 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 look at how to 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.

Locating elements using partial link text means that we locate 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. Whereas, 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.

In case the partial link text finds more than one anchor tag that contains the text being searched, it considers the first one only.

Step 1: Create a Project

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

The steps shown here are sufficient for all IDEs:

  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:

locating strategy

Navigating to Creating the New Project Option

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:

Project Configuration

Project Settings

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:

Screenshot-2024-03-10-210917

pom.xml

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.geeksforgeeks.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:

Screenshot-2024-03-11-183939

Link not found

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.geeksforgeeks.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:

Screenshot-2024-03-11-184744

Error Handling

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:

1. Chrome Driver

<!– https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver –><dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-chrome-driver</artifactId> <version>4.18.0</version></dependency>

2. IE Driver

<!– https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-ie-driver –><dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-ie-driver</artifactId> <version>4.18.0</version></dependency>

3. Safari Driver

<!– https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-safari-driver –><dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-safari-driver</artifactId> <version>4.18.0</version></dependency>

4. Microsoft Edge Driver

<!– https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-edge-driver –><dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-edge-driver</artifactId> <version>4.18.0</version></dependency>

5. Firefox Driver

<!– https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-firefox-driver –><dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-firefox-driver</artifactId> <version>4.18.0</version></dependency>

Just add any of these for the browser that you want to automate and you are good to go.

Conclusion

In this article, we have looked at how we can locate elements using partial link text. Please feel free to leave any comments in case you have any queries.

FAQs

1. What is the difference between partial link text and link text?

Link text refers to the entire text inside the anchor tags. You use link text when you know exactly what text you have to look for, meaning you want to locate the element that exactly matches the text. On the other hand, partial link text means that you have ‘partial text of the link’ and would like to locate elements that contain text that contain a somewhat similar version to what you have.

2. Can’t I use XPath to locate partial link text?

Yes it is possible to locate text using Xpath but it isn’t a recommended practice. Inbuilt functions offer security and performance better than custom-designed Xpath. Furthermore, partial link text functions in selenium offer a cleaner code alternative.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads