Detecting ASP.Net Server Error in Selenium

If you request a URL with Selenium, the first thing you want to check is whether the page can be displayed, or whether it generates a Server Error message.

If there a server error occurred, the string “Server Error in” occurs in the generated HTML text. We then strip everything that is not inside the <BODY> tag, remove all HTML tags (they do not contain any relevant information in case of an error), and log and store the remaining error message.


string sHtml = selenium.GetHtmlSource();
if (sHtml.Contains("Server Error in "))

    try    { sHtml = sHtml.Substring(sHtml.IndexOf("<BODY")); } catch { }
    try    { sHtml = sHtml.Substring(0, sHtml.IndexOf("</BODY>")); } catch { }
    try    { sHtml = Regex.Replace(sHtml, "<[^>]+?>", ""); } catch { }
    try    { sHtml = Regex.Replace(sHtml, " +", " "); } catch { }
    try    { sHtml = Regex.Replace(sHtml, @"(\s\n)+", "\n"); } catch { }

selenium is of type ISelenium, verificationErrors is a StringBuilder, conforming to the Selenium convention. NUnitLog is a custom class handling logging and error messages.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.