How to check URL for 404 using Selenium WebDriver?

We can check the URL for 404 using Selenium webdriver. A 404 check is actually done to verify if there are broken links in a page. On clicking such a link, we shall not be directed to the correct page.

A broken link can occur due to the following reasons −

  • The landing page is no longer available.

  • Some parts of the URL have been modified.

  • An incorrect URL has been specified on the page.

  • Firewall or geolocation limitations.

A URL can have the following status code −

  • 5XX − Represents issue in server.

  • 4XX − Represents resource cannot be determined.

  • 3XX − Represents redirection.

  • 2XX − Represents correct condition.

Thus we see that with only 2XX status code we can have a correct URL. We shall send a HTTP request and analyse its response code for all the links on the page.

Example

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
public class BrokenURL{
   public static void main(String[] args) throws
   InterruptedException{
      System.setProperty("webdriver.gecko.driver",
         "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
      WebDriver driver = new FirefoxDriver();
      // wait of 5 seconds
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      driver.get("https://www.google.com/");
      //get list of elements with anchor tag
      List l = driver.findElements(By.tagName("a"));
      //iterate links
      for(int j=0; j

Output

Updated on: 2021-01-30T12:49:14+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements