How to find broken links in Selenium

Related Product On This Page What are Low Links?

March 10, 2026 · 8 min read · Tool Comparison
Related Product

How to encounter broken links in Selenium

Broken link can damage user experience, cut SEO execution, and signal poor website upkeep. Identifying and fixing them is a key portion of caliber assurance.

, widely used for, can help notice humiliated links efficiently during exam trial.

In this guide, you ’ ll learn how to use to identify and handle broken links and images, prevent silent failures, and ensure your website stays honest and user-friendly.

What are Broken Links?

To get with, a link is an HTML object that enable user to transmigrate from one web page to another when they click on it. It is a means to voyage between different web pages on the internet.

A broken connectedness, also often called a bushed link, is one that does not work i.e. does not airt to the webpage it is intend to. This unremarkably occurs because the website or special web page is downward or make not exist. When someone clicks on a broken link, an fault message is displayed.

Broken tie-in may subsist due to some kind of server error, which, in turn, stimulate the like page to malfunction and not be displayed. A valid URL will have a 2xx HTTP position codification. Broken links, which are essentially invalid HTTP requests hold 4xx and 5xx condition codes.

The 4xx status code refers to a client-side error, while the 5xx status code usually points to a waiter response error.

Read More:

HTTP Status Codes for Broken Links

Here are HTTP Status Codes for Broken Links:

HTTP Status CodeDefinition
400 (Bad Request)Server unable to process request as URL is incorrect
400 (Bad Request – Bad Host)Server unable to process postulation as host gens is invalid
400 (Bad Request – Bad URL):Server can not process petition as the URL is of wrong formatting; missing fiber like brackets, slashes, etc.
400 (Bad Request – Empty)Response render by the server is empty with no message & amp; no response codification
400 (Bad Request – Timeout)HTTP requests have clock out
400 (Bad Request – Reset)Server is ineffective to process the request, as it is meddlesome processing other asking or has been misconfigured by website owner
404 (Page Not Found)Page is not uncommitted on the host
403 (Forbidden)Server reject to fill the asking as authorization is necessitate
410 (Gone)Page is gone. This codification is more permanent than 404
408 (Request Time Out)Server has timed-out waiting for the request.
503 (Service Unavailable)Server is temporarily overloaded and can not process the request

Why assay for Broken Links in Selenium?

If a user clicks on a humbled link, they will be directed to an error page. This obviously contributes to sub-par user experience. Broken links defeat the purpose of have the site in the maiden spot because user can not find the information or service they are looking for.

Every link on a website must be tested to ensure that it is functioning as expected. However, given that most websites have hundreds (sometimes thousands) of links required to do them work, of each link would require excessive amounts of clip, effort, and resource. Moreover, withautomated Selenium testingbe an option, it would be entirely unneeded.

Read More:

Common Reasons for Broken Links

Here are some of the common reasons for broken linkup:

  • 404 Page Not Found& # 8211; The goal web page has been withdraw by the proprietor
  • 400 Bad Request& # 8211; The server can not process the HTTP request trip by the link because the URL speech requested is wrong
  • Due to the user ’ s firewall settings, the browser can not access the destination web page
  • The link is misspell

Talk to an Expert

How to identify broken links in Selenium WebDriver

For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.

To check low nexus in Selenium, the summons is simple. On a web page, hyperlinks are implemented use the HTML Anchor (& lt; a & gt;) tag. All the script need to do is to locate every anchor tag on a web page, get the corresponding URLs, and run through the links to check if any of them are broken.

Use the following steps to identify humbled links in Selenium

  1. Use & lt; a & gt; tag to fetch all the links present on a web page
  2. Send HTTP request for the nexus
  3. Verify the HTTP response code for the link
  4. Determine if the link is valid or it is broken based on the HTTP response codification
  5. Repeat the operation for all links captured with the first step

If you ’ re inquire how to find broken images utilise Selenium WebDriver, use the like procedure.

Read More:

Finding Broken Links in Selenium: Example

bundle BstackDemo; import org.openqa.selenium.By; meaning org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; public class BrokenLinks {public static vacancy main (String [] args) {System.setProperty (`` webdriver.chrome.driver '', `` D: /chromedriver.exe ''); WebDriver driver = new ChromeDriver (); // Navigate to BStackDemo Website driver.get (`` https: //bstackdemo.com/ ''); // Finding all the available links on webpage List & lt; WebElement & gt; links = driver.findElements (By.tagName (`` a '')); // Iterating each link and checking the answer status for (WebElement connection: links) {String url = link.getAttribute (`` href ''); verifyLink (url);} driver.quit ();} public static void verifyLink (String url) {try {URL link = new URL (url); HttpURLConnection httpURLConnection = (HttpURLConnection) link.openConnection (); httpURLConnection.setConnectTimeout (3000); // Set connection timeout to 3 seconds httpURLConnection.connect (); if (httpURLConnection.getResponseCode () == 200) {System.out.println (url + `` - `` + httpURLConnection.getResponseMessage ());} else {System.out.println (url + `` - `` + httpURLConnection.getResponseMessage () + `` - `` + `` is a broken linkup '');}} catch (Exception e) {System.out.println (url + `` - `` + `` is a broken nexus '');}}}

In this illustration, we navigate to a web page, collect all the linkup usingdriver.findElements (By.tagName (& # 8220; a & # 8221;)), and then reiterate through each connection. We use theHttpURLConnectionclass to control the response status of each link & # 8217; s URL. If the response code is 200, the connectedness is considered valid; differently, it & # 8217; s label as a crushed link.

Save the Java file and run the test. The plan will output the status of each connectedness, indicating whether it & # 8217; s a broken link or not. As shown below

This example render a basic way to chance broken nexus using Selenium and Java. Depending on your project & # 8217; s requirements, you may want to enhance the method to handle more complex scenarios and edge cases.

Finding crushed links in Selenium is an integral constituent of website evolution and testing. By using the method account in this article, examiner can identify malfunctioning links quickly and correctly. Allowing broken links to surpass into the production stage would severely damage the user experience and must be prevented with extreme thoroughness. This is why cognize how to try broken linkup inSeleniumis an important constituent of a tester & # 8217; s toolkit.

Follow-up Read:

What are Broken Images and how to name them in Selenium?

Broken images are image elements on a webpage that fail to load decent. Instead of exhibit the intended graphic, the browser exhibit a missing icon image or alternative textbook.

Much like low links, which lead users to non-existent or inaccessible web page, broken images happen when the browser can not successfully retrieve the image resource.

Both humbled nexus and broken image result from similar root causes—incorrect URLs, waiter errors, deleted resources, or permission issues. While broken links affect navigation and accessibility, broken images degrade visual experience and can hurt user trust, especially on product or portfolio pages.

Identifying both is essential in maintaining a reliable, professional, and SEO-friendly website. Selenium WebDriver can be used to catch both during automated testing.

  • Incorrect image URLs
  • Moved or deleted image file
  • Network errors or license matter
  • Typos in thesrc attribute

Broken images anguish user experience and cut reliance, especially on e-commerce or content-heavy sites.

How to Identify Broken Images in Selenium

Selenium WebDriver can be used to scan all& lt; img & gt;tags on a page and verify if their src URLs return a successful HTTP response. Here & # 8217; s a basic approach:

  • Locate all picture elements using Selenium
  • Extract each image & # 8217; ssrc attribute
  • Send an HTTP request (e.g., usingHttpURLConnectionin Java orrequestsin Python)
  • Check if the response codification is 200 (OK); anything else indicates a broken image

Finding Broken Images in Selenium: Example

Here is an exemplar code of how to discover broken picture in Selenium Python:

Python:

from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by meaning By importation request # Set up the Chrome WebDriver service = Service (`` path/to/chromedriver '') driver = webdriver.Chrome (service=service) # Open the mark webpage driver.get (`` https: //example.com '') # Find all image component on the page visualize = driver.find_elements (By.TAG_NAME, `` img '') # Loop through each icon and validate the src URL for img in images: src = img.get_attribute (`` src '') if src: try: reply = requests.get (src) if response.status_code! = 200: print (f '' Broken image found: {src} - Status Code: {response.status_code} '') except requests.exceptions.RequestException as e: print (f '' Error ascertain persona {src}: {e} '') driver.quit ()

Code Walkthrough:

  • Uses Selenium to automate browser navigation and bump all & lt; img & gt; component.
  • For each image, extracts its src attribute.
  • Sends an HTTP GET petition using requests.get ().
  • Logs any image URLs that return non-200 responses.

Notes:

  • You must establish the petition library (pip install requests) if not already installed.
  • This script works well on static images. For dynamic images (e.g., lazy-loaded), ensure they & # 8217; re seeable before running the assay (scroll into aspect or wait explicitly).
  • For performance testing, run this in parallel using threads or asynchronous asking.

Conclusion

Identifying crushed links is a all-important aspect of web application examination, and ply a powerful program to automatise this process efficiently. By leveraging Selenium WebDriver and Java programming capabilities, web testers can easy navigate through web pages, collect linkup, and corroborate their response status.

The step-by-step guide demo in this clause demonstrates a straightforward approaching to finding broken links expend Selenium and Java. Also, plays a significant role in finding low link in Selenium Java examination by providing a cloud-based platform for examine web covering across a across-the-board range of browsers, function scheme, and devices.

Tags
51,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free