Selenium

How to Find All Links, Images & Broken Links in Selenium WebDriver (Without Opening Browser)

Find All Links, Images & Broken Links in Selenium 🚀

By Bhau Automation • Advanced Selenium Tutorial

🎯 What You Will Learn

  • How to extract all URLs from a webpage
  • How to find all image links
  • How to detect broken links
  • How to detect broken images
  • How to do it without opening a browser

📌 Why This is Important?

Broken links and images affect SEO ranking and user experience. Automation helps testers quickly identify issues in large websites.

💡 Detecting broken links is a common real-time automation interview question.

🔗 Find All Links from Webpage

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

for(WebElement link : links){
    System.out.println(link.getAttribute("href"));
}

🖼️ Find All Image Links

List images = driver.findElements(By.tagName("img"));

for(WebElement img : images){
    System.out.println(img.getAttribute("src"));
}

❌ Find Broken Links

URL url = new URL(link);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("HEAD");
connection.connect();

int responseCode = connection.getResponseCode();

if(responseCode >= 400){
    System.out.println("Broken Link: " + link);
}

❌ Find Broken Images

URL url = new URL(imageSrc);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();

if(connection.getResponseCode() >= 400){
    System.out.println("Broken Image: " + imageSrc);
}

⚡ Without Opening Browser

You can use HttpURLConnection directly to test URLs without launching a browser, making execution faster.

🌍 Real-World Use Cases

  • SEO audit automation
  • Website health check
  • Regression testing
  • Large website validation

🎥 Watch Full Video

👉 Watch Complete Tutorial

🎓 Key Takeaways

  • Links and images can be extracted using tagName
  • Broken links detected using HTTP response codes
  • No browser needed for faster execution
  • Important for automation testers & SEO

🚀 Created with ❤️ by Bhau Automation

Back to All Articles