Finding Broken Links in Selenium
The implementation process can be developed into many segments. Here, the main structure of the program is divided into some parts. This will help to better understand the approach.
1. Import Packages: First, we need to import the necessary packages into the program. It is advisable to use the Eclipse IDE for easy implementation. The Eclipse IDE will automatically identify the packages. Then it imports them to a certain program. Here, we need to import the packages which are associated with the WebDrivers, Specific WebDriver, Web Elements, Some types of exceptions, etc. All these packages need to be imported into the program before executing it.
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
2. Collect all links on the web page: Here, we need to take one web page as an example. There we need to collect all the links present on that web page. For that purpose, we need to insert all the links into the list structure. We have to find the links with the tag name ‘a’. As all the links in one web page have the ‘a’ tag. This will help to distinguish it better. We have to use the findelement() method to get all the links. Then we have to make a type casting of the list to the WebElement format. There we have to store all the links.
List <WebElement>links = driver.findElements(By.tagName(“a”));
3. Identify and Validate URLs: Then we have to validate each & every link stored in the List. There we need to run a loop. Inside the loop, we have to identify each & every link. We have to use the getAttribute() function to identify the proper link stored in the list. Along with the link, there are some other values that also get stored inside the List. So, we have to only identify the link. As the links have the ‘href’ attributes, so we have to use them. Then we have to make an object of the URL type. This will help in the next step.
WebElement element = links.get(i);
String url=element.getAttribute(“href”);
URL link=new URL(url);
4. Send HTTP request: Now, we have to create a connection. For that purpose, we need to use the HttpURLConnection object. We need to create one object of HttpURLConnection. Using that object we can connect the link over the internet. We have to use the connect() method to do the same. This will run the links on the internet. Based on the execution, it sends one code back to the code. This is the response code of the link.
HttpURLConnection httpConn = (HttpURLConnection) link.openConnection();
httpConn.connect();
5. Validate Links: Now, we have to run an if-else statement. If the response code is greater or equal to 400, then the link is a broken one. Or else if the response code is lower than that, then the link can’t be a broken one. This shows that there is not any server problem with the link.
if(code >= 400)
{
System.out.println(“Broken Link: “+url);
}
else
{
System.out.println(“Valid Link: “+url);
}
Below is the implementation to detect broken links on the webpage:
Java
// Java program to detect broken // links on the webpage // Imported Packages import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class BrokenLinks { public static void main(String[] args) throws IOException { // Providing The Location Of ChromeDriver System.setProperty( "webdriver.chrome.driver" , "E:\\ECLIPSE WORKSPACE\\w3wiki\\resources\\chromedriver.exe" ); WebDriver driver = new ChromeDriver(); // Maximizing Driver Window driver.manage().window().maximize(); // Checking The Google Link driver.get( "http://www.google.co.in/" ); // Getting All The Links In One List List <WebElement>links = driver.findElements(By.tagName( "a" )); // Printing The Total Links Number System.out.println( "Total Link Size: " + links.size()); for ( int i = 0 ; i < links.size(); i++) { // Checking Each & Every Links WebElement element = links.get(i); String url = element.getAttribute( "href" ); URL link = new URL(url); HttpURLConnection httpConn = (HttpURLConnection) link.openConnection(); httpConn.connect(); // Getting The Response Code int code = httpConn.getResponseCode(); // If The Number Is Greater Than 400, // Then It Is Broken Link if (code >= 400 ) { System.out.println( "Broken Link: " + url); } else { System.out.println( "Valid Link: " + url); } } System.out.println(); System.out.println( "All Links Checked" ); // Closing The Driver driver.quit(); } } |
Output:
How Selenium WebDriver Can be Used to Detect Broken Links?
Selenium is a widely used tool for testing any websites or other applications. It is a suite of all testing software. There are many segments of Selenium. Like there is Selenium Web Driver, Selenium IDE, Selenium RC, etc. Selenium IDE is used by those users who are coming from a non-computer science background. As in Selenium IDE, there is no need for programming knowledge. It can be simply used. But in the case of Selenium Web Driver, programming knowledge is important. Selenium Webdriver can be used in Java programming language or Python programming language. The article focuses on discussing How to detect broken links in Selenium WebDriver.
The following topics will be discussed here:
- What are Broken Links?
- Why Check for Broken Links in Selenium?
- Common Reasons for Broken Links.
- How to identify broken links in Selenium WebDriver.
- Finding Broken Links in Selenium.
Let’s start discussing each of these topics in detail.
Contact Us