How to identify broken links in Selenium WebDriver: Python Example

Here's an updated Selenium Python program that checks for both missing and invalid links:

import requests
from selenium import webdriver

# Create a new instance of the Firefox driver
driver = webdriver.Firefox()

# Navigate to the web page you want to check
driver.get("http://www.example.com")

# Find all the links on the page
links = driver.find_elements_by_tag_name("a")

# Loop through each link and check if it is missing or invalid
for link in links:
    href = link.get_attribute("href")
    if href is None or href == "":
        print("Missing link: ", link.text)
    else:
        response = requests.head(href)
        if response.status_code >= 400:
            print("Invalid link: ", href)

# Close the browser window
driver.quit()

This updated program still uses Selenium to navigate to the web page and find all the links. However, it also uses the requests module to check the validity of each link.

In the loop that iterates over each link, the program first checks if the link is missing or empty, just like before. If the link is not missing, it sends a HEAD request to the link using the requests.head() method. This returns a response object that contains information about the link, including the status code.

If the status code is 400 or higher, the link is considered invalid, and the program prints a message to the console indicating that the link is invalid.

Finally, the program closes the browser window using the quit() method, just like before 

Comments

Popular posts from this blog

Selenium: File download handling.

Major Differences between Python and Java

Cypress V/S Selenium