Locating Strategies By Partial Link Text Using Java
Last Updated :
05 Nov, 2024
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.
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:
- Maven is a build tool that fetches external libraries that are needed by the Java Runtime Environment.
- 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 TextStep 2: Set Up the Project Configuration
Next, we will look at the configuration settings for our project.
- When you click the Project button from the previous step, a new dialogue box will open.
- 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 TextStep 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 TextStep 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:
- Open the Browser
- Locate the link we want to visit using the link text
- Navigate to the link
- Close the Browser
Without any human interactions. Modify the code in your main class as follows:
Main,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:
To avoid such situations, we can enclose our code in a try-catch block for a cleaner execution.
Modify the code above as:
Main.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:
Output of Error Handling with Try and catch blockConclusion
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.
Similar Reads
Locating Strategies By Tag Name 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. Table of Content Steps for Locating Elements By Tag Name in JavaConclusionFAQ's on Locating Stra
6 min read
Locating Strategies By ID Using Java
Here we are going to discuss locating a particular web element using the value of its id Table of Content Steps for Locating Strategies By ID Using JavaConclusionFAQ'sSteps for Locating Strategies By ID Using JavaStep1:Create a Java Project In Eclipse IDELaunch Eclipse IDE and create a new project b
3 min read
Locating Strategies By Class Name Using Java
Efficiently locating elements by their Class Name is important for successful automation testing in Java. In this article, we explore the use of Selenium's By.className() method to locate web elements, providing step-by-step guidance on setting up a Java project, adding Selenium JAR files, and demon
4 min read
Locating Strategies By XPath 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 article, we will explore how to use XPath to locate elements on our HTML code u
4 min read
How to Get All Available Links on the Page using Selenium in Java?
Selenium is an open-source Web-Automation tool that is used to automate web Browser Testing. The major advantage of using selenium is, that it supports all major web browsers and works on all major Operating Systems, and it supports writing scripts on various languages such as Java, Â JavaScript, C#
2 min read
Selenium Locating Strategies
Selenium is one of the most powerful and widely used tools for automated web applications. Whether you're a software developer or a QA tester selenium is an important tool to have in your toolkit. One of the most important tasks in order to automate the web applications is interacting with the eleme
12 min read
find_element_by_partial_link_text() driver method - Selenium Python
Selenium's Python Module is built to perform automated testing with Python. Selenium Python bindings provide a simple API to write functional/acceptance tests using Selenium WebDriver. After you have installed selenium and checked out - Navigating links using get method, you might want to play more
3 min read
Java Program to Goto a Link Using Applet
In this article, we shall be animating the applet window to show the goto link in Java Applet. A Goto link is basically, any particular link that redirects us from one page to another. Approach to Implement Goto a Link using AppletCreate an applet with the two buttons.Add ActionListener to the butto
2 min read
C# Program to Print the Names that Contain 'MAN' Substring Using LINQ
LINQ is known as Language Integrated Query and it is introduced in .NET 3.5. It gives the ability to .NET languages to generate queries to retrieve data from the data source. It removes the mismatch between programming languages and databases and the syntax used to create a query is the same no matt
2 min read
Locating single elements in Selenium Python
Locators Strategies in Selenium Python are methods that are used to locate elements from the page and perform an operation on the same. Seleniumâs Python Module is built to perform automated testing with Python. Selenium Python bindings provide a simple API to write functional/acceptance tests using
5 min read