top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Handling Broken links in Selenium


Image by Andrew Martin from Pixabay

In this blog I wanted to show how to handle broken links in selenium.

Let us consider a webpage with several links, if we have to check if each and every link is working or not manually, it's a difficult process. With the help of selenium automation scripts, we can handle this efficiently.

Before we start with the selenium code, it’s good to know the response codes returned by server.

For any URL if we click the link, a HTTP request is sent to the server from browser & server processes this request and will send a response back to the browser. If there are no errors on load, a response code of 200 OK is sent. We can find the status codes as below go to inspect on the webpage and choose Network



If response 4XX or 5XX page not found from server, it means the link is broken i.e., broken link.



So, if we inspect the Elements in a page we can find anchor tags, all the hyperlinks have anchor tags, and we can use logic to handle all the links as shown in below code .as we have to retrieve multiple links, we are using driver.findElements(By.tagname (“a”)), it will return list of links.

we need to iterate using for each loop for each and every link and we need to get the link URL and HttpURLconnection is predefined java class, open connection (), method that opens connection to server ,huc.connect it will send req to server.


We can check in this way to find if links are valid or not.

Thank you for reading my blog.

261 views0 comments
bottom of page