Monday, March 6, 2017

Selenium WebDriver - Find Broken Links Using C#

Selenium WebDriver - Find Broken Links Using C#


In order to find the broken links using Selenium WebDriver we are using below example HTML page.

Code for our example HTML page:


HTML page opened in a browser:


Code used to find the broken links:


Using Firefox we are navigating to the page on which we need to verify all links. We get all links on a page using "a" tag. Next we are iterating through the list of links and printing the Error code for each on the Console.

HttpWebRequest Class provides an HTTP-specific implementation of the WebRequest Class. In the above code we are initializing a new instance of the HttpWebRequest Class. We are using HttpWebRequest Class to make a request to a Uniform Resource Identifier (URI). Similarly HttpWebResponse Class provides an HTTP-specific implementation of the WebResponse Class. This line returns an HttpWebResponse from an HttpWebRequest.

Result of running the above code: