Selenium is an open-source web-based automation tool. We'll learn how to find the broken links in the web page using selenium in Python.
We'll follow the steps mentioned below to find the broken links:
from selenium import webdriver from selenium.webdriver.common.by import By import time import requests #specify where your chrome driver present in your pc PATH=r"C:\Users\educative\Documents\chromedriver\chromedriver.exe" #get instance of web driver driver = webdriver.Chrome(PATH) #provide website url here driver.get("http://demo.guru99.com/test/newtours/") #get all links all_links = driver.find_elements(By.CSS_SELECTOR,"a") #check each link if it is broken or not for link in all_links: #extract url from href attribute url = link.get_attribute('href') #send request to the url and get the result result = requests.head(url) #if status code is not 200 then print the url (customize the if condition according to the need) if result.status_code != 200: print(url, result.status_code)
chromedriver.exein the windows environment.
driver.get()method to open it.
find_elements()method to get all links present on the current web page.
for-inloop to loop through each link returned in the above step.
200then we consider it as a broken link and print it. We can also customize this condition according to our needs.
View all Courses