How can I write code for identifying and reporting broken links by using selenium?

237    Asked by behail_3566 in QA Testing , Asked on Jun 24, 2024

I am currently engaged in a particular task which is related to using selenium for automation and I need to ensure that all links on a page are working correctly. How can I write code for identifying and reporting broken links on the webpage by using the Selenium WebDriver? 

Answered by Elizabeth Clark

 In the context of selenium, you can find the broken links on the webpage by using the selenium by using the steps which are given below:-

Identify the links

You can use the Selenium WebDriver to find all the links on the webpage.

Iterate through link

You can now iterate through each link and extract to its URL.

Send HTTP request

You can send an HTTP request for each URL and check the response code.

Identify the broken links

You can now identify the link with the response code which indicates an error.

Report broken links

You can also report the broken links or log in to the details of broken links to get insight.

Here is the sample coding given in Java programming language to accomplish the above steps:-

Import org.openqa.selenium.By;
Import 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 BrokenLinksExample {
    Public static void main(String[] args) {
        // Set the path to the ChromeDriver executable
        System.setProperty(“webdriver.chrome.driver”, “/path/to/chromedriver”);
        // Initialize WebDriver
        WebDriver driver = new ChromeDriver();
        // Navigate to the webpage
        Driver.get(https://example.com);
        // Find all anchor tags (links) on the webpage
        List links = driver.findElements(By.tagName(“a”));
        // Iterate through each link
        For (WebElement link : links) {
            // Get the URL of the link
            String url = link.getAttribute(“href”);
            If (url != null && !url.isEmpty()) {
                // Send HTTP request and check response code
                Int statusCode = getResponseCode(url);
                If (statusCode != 200) {
                    System.out.println(“Broken Link Found: “ + url + “ (Status Code: “ + statusCode + “)”);
                }
            }
        }
        // Close the WebDriver
        Driver.quit();
    }

    // Method to send HTTP request and get response code

    Public static int getResponseCode(String urlString) {
        Try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            Connection.setRequestMethod(“GET”);
            Connection.connect();
            Int responseCode = connection.getResponseCode();
            Connection.disconnect();
            Return responseCode;
        } catch (Exception e) {
            Return -1; // Return -1 if unable to get response code
        }
    }
}

Here is the coding given in python programming language:-

From selenium import webdriver
Import requests
# Set the path to the ChromeDriver executable
Chrome_driver_path = “/path/to/chromedriver”
# Initialize ChromeDriver with the specified path
Driver = webdriver.Chrome(executable_path=chrome_driver_path)
# Navigate to the webpage
Driver.get(https://example.com)
# Find all anchor tags (links) on the webpage
Links = driver.find_elements_by_tag_name(“a”)
# Iterate through each link
For link in links:
    # Get the URL of the link
    url = link.get_attribute(“href”)
    if url:
        # Send HTTP request and check response code
        Response = requests.head(url)
        If response.status_code != 200:
            Print(f”Broken Link Found: {url} (Status Code: {response.status_code})”)
# Close the WebDriver
Driver.quit()

Here is the example given in HTML:-




Your Answer

Interviews

Parent Categories