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 webdriverfrom selenium.webdriver.common.by import Byimport timeimport requests#specify where your chrome driver present in your pcPATH=r"C:\Users\educative\Documents\chromedriver\chromedriver.exe"#get instance of web driverdriver = webdriver.Chrome(PATH)#provide website url heredriver.get("http://demo.guru99.com/test/newtours/")#get all linksall_links = driver.find_elements(By.CSS_SELECTOR,"a")#check each link if it is broken or notfor link in all_links:#extract url from href attributeurl = link.get_attribute('href')#send request to the url and get the resultresult = 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.exe
in the windows environment.webdriver
.driver.get()
method to open it.find_elements()
method to get all links present on the current web page.for-in
loop to loop through each link returned in the above step.200
then we consider it as a broken link and print it. We can also customize this condition according to our needs.