October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk7 min

How to Test for Broken Links with Selenium

Selenium is best for checking selected links in real browser journeys—not crawling an entire site. Learn how to assert the destination, handle dynamic pages and choose an HTTP crawler for broad coverage.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Selenium can test whether a link works as part of a real browser journey, but it is not the right tool for crawling every link across a site. For a user-facing test, click or navigate to the destination and assert that the expected page appears. For a site-wide link inventory, use an HTTP crawler; Selenium’s own guidance recommends alternatives such as curl or BeautifulSoup because starting a browser and traversing the DOM adds overhead.

What Selenium can—and cannot—tell you about a broken link

Selenium WebDriver drives a browser in a way that represents a user interacting with a website. That makes it useful for checking rendered pages, navigation and visible error states. It does not include a built-in broken-link checker, and Selenium advises against using WebDriver to spider links across a site.

As an Amazon Associate I earn from qualifying purchases.

The distinction is the test’s purpose:

  • User-journey test: verify that a particular link takes a user to the expected page or that a failure produces an understandable error page. Selenium is a good fit.
  • Site-wide inventory: discover and request many links, including links users may never click in a test flow. Use a crawler or HTTP-based approach instead.

Selenium’s link-spidering guidance specifically suggests curl or BeautifulSoup for link discovery and checking. Browser automation can still be used to test selected links that matter to a user journey.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Test a link as part of a real user journey

For a functional test, begin at the page a user would visit, follow the link, and assert a meaningful feature of the destination. Prefer a stable heading, page title, or other reliable element over merely checking that the browser changed URLs: a destination can load and still show an error.

  1. Open the page containing the link.
  2. Wait for the link to be present and interactable if the page renders it asynchronously.
  3. Click the link and wait for the destination or its identifying content.
  4. Assert that the expected destination content is visible. If the destination is an error page, assert its title or a reliable element such as its H1.

Selenium’s guidance on HTTP response codes notes that, for functional testing, the user steps preceding a failure are usually more important than the status code alone. Checking the rendered result tests what the user experiences; it does not establish the HTTP response status or enumerate other links on the site.

Python example

This example uses Selenium’s Python API. Replace the page URL, link locator and expected heading with values from your application. It assumes a Selenium WebDriver setup for the browser you want to test.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

start_url = "https://example.com/help"
expected_heading = "Getting started"

# Configure the driver for the browser used by your test environment.
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)

try:
    driver.get(start_url)

    help_link = wait.until(
        EC.element_to_be_clickable((By.LINK_TEXT, "Getting started"))
    )
    help_link.click()

    heading = wait.until(
        EC.visibility_of_element_located((By.TAG_NAME, "h1"))
    )
    assert heading.text.strip() == expected_heading, (
        f"Unexpected destination heading: {heading.text!r}"
    )
finally:
    driver.quit()

The example checks one known journey, not every anchor on the starting page. If the destination contains multiple H1 elements or the application uses a more stable test identifier, use a locator specific to the expected destination instead of the generic H1 locator.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Wait for JavaScript-generated links and content

A browser reporting that the initial document is ready does not mean a JavaScript application has finished changing the page. Links may be inserted later, and a destination’s content may arrive after navigation. Selenium’s waiting strategies explain why tests should wait for the condition they need rather than assume page readiness means all scripts are done.

  • Use an explicit wait for the link to become present or clickable before clicking it.
  • After navigation, wait for the expected destination element to become visible.
  • Choose a timeout appropriate to the application and test environment; a timeout is a limit for the wait, not proof that the link is broken.
  • Avoid adding arbitrary sleeps unless there is a specific timing reason; condition-based waits express what the test actually needs.

When HTTP status codes matter

A visible page assertion answers whether the user reached the expected experience. It does not tell you whether the response was, for example, an HTTP 404 or whether a redirect occurred along the way. Selenium documents a proxy as an advanced way to capture response information during browser testing, while noting that browser support for exposing response codes varies. Use that route only when status-code evidence is a real requirement of the test, and verify that the chosen browser and proxy setup expose the responses you need.

WebDriver BiDi can stream browser events, including network requests, console messages and JavaScript errors. Those events can help investigate a browser flow, but they are observability capabilities—not a documented, one-step recipe for crawling and validating every link on a site.

For a site-wide broken-link inventory, use an HTTP crawler

If the goal is to find links across many pages, use a crawler or HTTP-based workflow rather than launching a browser for every link. At a high level, a crawler can collect links from pages, normalize their URLs, request targets and report failures. Decisions such as which status codes count as failures, how to handle redirects, and how to limit crawling belong to the crawler’s design and should be defined for your site.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

HTTP crawling and Selenium answer different questions. A crawler is suited to broad link discovery and requests without browser startup and DOM traversal. Selenium is suited to verifying what happens when a user follows selected links, including content created or changed in the browser. If links only appear after client-side interaction, a crawler may not discover them from the initial HTML; test those important flows in Selenium or choose a crawling approach that renders pages.

Performance, reliability and scope

  • Runtime overhead: WebDriver starts and drives a browser and traverses the DOM, which Selenium identifies as a reason not to spider links with it. The documentation provides no general speed-up figure; actual runtime depends on the pages and test setup.
  • Coverage: A user-flow test covers only the paths and links it exercises. It is not a complete site inventory.
  • Dynamic pages: JavaScript timing can make a link or its destination content appear later, so condition-based waits are part of a reliable test.
  • Failure diagnosis: A failed assertion may reflect a broken destination, a synchronization problem, or an underlying browser-driver issue. Isolate the cause before treating every failure as a broken link.

Troubleshoot a failing Selenium link test

The link cannot be found

It may not exist in the initial document yet, or the locator may not match the rendered element. Wait for the expected element, then verify the locator against the page’s actual DOM and accessible link text.

Rank #4
The Web Testing Handbook
  • Used Book in Good Condition

The test times out after clicking

The destination may be slow, its expected element may differ from the assertion, or JavaScript may still be updating the page. Wait for a destination-specific condition and confirm that the test is checking the right element rather than assuming navigation alone is success.

The browser shows an error, but the cause is unclear

Check the rendered title or a reliable error-page element to distinguish a user-visible failure from a successful destination. If you specifically require the HTTP response status, consider the documented proxy approach and account for browser support differences.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Failures vary by browser or driver

Selenium’s troubleshooting guidance cautions that synchronization and the underlying browser driver can both cause failures. Reproduce the issue with another supported browser where practical to help isolate a browser- or driver-specific problem from a site defect.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If you need a screenshot of the destination as part of a check or report, ScreenshotNeo is a website screenshot API and MCP server. A single GET request returns a screenshot or PDF; its clean-shot workflow removes known consent banners, newsletter popups and chat widgets before capture. Only clean shots are billed: bot checks, blank pages, timeouts, failed loads and cache hits cost nothing, and responses identify the page verdict and billing status. Its MCP server lets AI agents take screenshots, and the free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000.

For example, save a WebP screenshot of the destination URL:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com/help -o shot.webp

See the ScreenshotNeo API documentation for parameters and response details. A screenshot can help inspect a rendered page, but it does not replace Selenium assertions or a site-wide link crawler. Sign up for 1,000 free screenshots a month with no card.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Frequently Asked Questions

Does Selenium have a built-in broken-link checker?

No. WebDriver automates browser interactions; Selenium advises against using it to spider links across a site.

Can Selenium tell me a link’s HTTP status code?

Not as a simple, universally supported built-in check. Selenium documents a proxy as an advanced option, with browser support varying.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.