Site Loader
Auckland, New Zealand
In this post we will discuss one of the common question asked by many readers of ExecuteAutomation, which is the topic name. Its actually fairly very simple though, I just wrote 10 lines of code to achieve it (you might have even better solution than one I have) So, here is the pseudo code
  1. Navigate to the URL of site you are interested in
  2. Get all the links of page using FindElements(By.Tag(“a”)) method
  3. Iterate through the page URLs and get the attributes href using GetAttribute(“href”) method
  4. Use Simple WebRequest and HttpWebResponse class to get the page response and status code.
So, here is the output

Here is the source code Thanks for reading the post and watching the video!!! Please leave your comments and let me know if there is anything I should update in this post. Thanks, Karthik KK

Post Author: Karthik kk

9 Replies to “Identifying broken links with Selenium C#”

  1. Karthik, I’m running this code for my website but for some reason it fails after 2 iterations through the foreach loop. I’ve verified that it is not that link by skipping it. No matter what the 3rd link it is it fails and throws “Object Reference not set to an instance of an object. Any Ideas?

    1. Yep.Strange .Me too facing the same problem. Every 3rd iteration fails.

      Whats the alternative solution ?/

      1. Hi,

        Did you find a solution for the problem? I am facing the exact issue, after 2 iterations the loop is over. Can’t figure it out what the problem might be.


  2. “Object Reference not set to an instance of an object” error message is displayed when you try to perform any action with an object which is set as “null”. In this case I don’t know what could it be (“href”, “url”, etc..). It will depend of the page that you are testing. The best way to figure out it, is debugging at the beginning of “foreach” step by step.

    Maybe try it:

    foreach (var url in urls)
    if (!(url.Text.Contains("Email") || url.Text == "" || url.Text == null))
    var href = url.GetAttribute("href");
    if (href == null)
    System.Console.WriteLine("Without URL");
    //Get the url
    re = (HttpWebRequest)WebRequest.Create();
    var response = (HttpWebResponse)re.GetResponse();
    System.Console.WriteLine($"URL: {url.GetAttribute("href")} status is :{response.StatusCode}");
    catch (WebException e)
    var errorResponse = (HttpWebResponse)e.Response;
    System.Console.WriteLine($"URL: {url.GetAttribute("href")} status is :{errorResponse.StatusCode}");

    Note: I didn’t try it, just a suggestion.

Leave a Reply

Your email address will not be published. Required fields are marked *