Broken links in Selenium
![Broken links in Selenium Broken links in Selenium](https://www.h2kinfosys.com/blog/wp-content/uploads/2024/06/Copy-of-ba-online-training.png)
The link in an HTML object that enables users to migrate from one web page to another when it is click on it. It means to navigate between different webpage on the internet. A broken link will be often called as a dead link. It is the one that does not work, it does not direct or redirect to the web page it is meant to. This is usually occurs because the website or particular web page will down or does not exist. When one clicks on a broken link, an error message will be displayed. Broken link will exist due to some kind of server error which in turn causes the corresponding page to malfunction and also not yet to be displayed. A valid URL will have 2xx HTTP status code. Broken link, which essentially will have invalid HTTP requests have 4xx and 5xx status codes.
Why we have to check for broken link in selenium?
On clicking a broken link, users are directed to an error page. This will obviously contribute to sub par user experience. Broken links will defeat the purpose of having the website in the first place because users who cannot find the information or service they are looking for. Every link in website must be tested to make sure that it is functioning as expected. That will give the most website that have broken links hundreds of links that are required to make them work. Manual testing of each link that would need excessive amounts of time, effort and resources.
There are common reasons to identify broken links in a selenium web driver:
404 page not found-Here the destination web page will be removed by the owner
400 Bad Request-The server may not processes the HTTP request which is triggered by the link as the URL address requested will be wrong.
Due to the user’s firewall settings the browser will not access the destination of the web page.
The link is misspelled. All these are the reasons for the broken link.
How we can identify the broken links in selenium Web driver?
We have to check broken links in selenium, the process is simple. Here the simple web page hyperlinks are implemented using the HTML Anchor(<a>) tag. All the script needs to do is locate every anchor tag will be on a web page that is getting corresponding URLs and run through the links to check if any of them are broken.
By using the following steps to identify broken links in selenium
- Collect all the links present on web page depend on a <a> tag
- Send the HTTP request for each link.
- Verify the HTTP response code.
- Determine if the link will be broken that is based on the HTTP response code.
- Repeat the process for any links will be captured with the first step.
Example for broken link
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class BrokenLinks {
public static void main(String[] args) throws InterruptedException//throws an exception
{
System.setProperty(“webdriver.gecko.driver”, “C:\\Selenium Environment\\Drivers\\geckodriver.exe”);
WebDriver driver = new FirefoxDriver();
//Maximize the browser
driver.manage().window().maximize();
//Implicit wait for 10 seconds
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//To launch iitworkforce .com
driver.get(“https://www.iitworkforce.com”);
//Wait for 5 seconds
Thread.sleep(5000);
//We are using tagName method to collect the list of items with tagName “a”
//findElements – to search all the elements within the current page. It returns a list of all possible webelements or an empty list if nothing matches
List<WebElement> links = driver.findElements(By.tagName(“a”));
//To print the total number of links
System.out.println(“Total links are “+links.size());
//used for loop to
for(int i=0; i<links.size(); i++) {
WebElement element = links.get(i);
//we use “href” attribute, we could get the url of the requried link
String url=element.getAttribute(“href”);
//calling verifyLink() method here. by passing the parameter as url which we collected in the above link
//we can know detailed functionality of the verifyLink(url) method below
verifyLink(url);
}
}
// here the function verifyLink(String urlLink) verifies any broken links and return the server status.
public static void verifyLink(String urlLink) {
//Sometimes we may face exception “java.net.MalformedURLException”. we can keep code in try catch block to continue the broken link analysis
try {
//we can use URL Class – Create object of the URL Class and pass the urlLink as parameter
URL link = new URL(urlLink);
// Create a connection by using URL object (i.e., link)
HttpURLConnection httpConn =(HttpURLConnection)link.openConnection();
//Set the timeout for 2 seconds
httpConn.setConnectTimeout(2000);
//connect using connect method
httpConn.connect();
// by using getResponseCode() to get the response code.
if(httpConn.getResponseCode()== 200) {
System.out.println(urlLink+” – “+httpConn.getResponseMessage());
}
if(httpConn.getResponseCode()== 404) {
System.out.println(urlLink+” – “+httpConn.getResponseMessage());
}
}
//getResponseCode method will return = IOException – suppose error occurrs connecting to the server.
catch (Exception e) {
//e.printStackTrace();
}
}
}
Questions
- What are the broken links?
- Why is it important to identify broken link in selenium?