in Selenium by
Q:
How do you find broken links in Selenium WebDriver?

► Click here to show 1 Answer

0 votes
by
When we use driver.get() method to navigate to a URL, it will respond with a status of 200-OK

200 – OK denotes that the link is working and it has been obtained. If any other status is obtained, then it is an indication that the link is broken.

Some of the HTTP status codes are :

200 – valid Link

404 – Link Not Found

400 – Bad Request

401 – Unauthorized

500 – Internal error

As a starter, obtain the links from the web application, and then individually get their status.

Navigate to the interested webpage for e.g. www.amazon.com

Collect all the links from the webpage. All the links are associated with the Tag ‘a‘

  List<WebElement> links = driver.findElements(By.tagName("a"));

Create a list of type WebElement to store all the Link elements in it.

  for(int i=0; i<links.size(); i++) {

           WebElement element = links.get(i);

                      String url=element.getAttribute("href");

                       verifyLink(url);  }

Now Create a Connection using URL object( i.e ., link)

  URL link = new URL(urlLink);

Connect using Connect Method

   HttpURLConnection httpConn =(HttpURLConnection)link.openConnection();

Use getResponseCode () to get response code

    if(httpConn.getResponseCode()!== 200)

Through exception, if any error occurred

    System.out.println(“Broken Link”);
Selenium
Learn More with Madanswer

Related questions

0 votes
asked Apr 13, 2020 in QuickTest Professional (QTP) by amita rallin
...