Verifying the canonical tag on a web page using Selenium GetLocation

Here is an example to verify the canonical tag on a web page using Selenium GetLocation. The selenium script is written in C#

public int CheckCanonicalTag()
{
int resultcount = 0;
string URL = selenium.GetLocation();
if (selenium.IsElementPresent("//link[@rel='canonical' and @href='" + System.Web.HttpUtility.UrlDecode(URL) + "']"))
{
Console.WriteLine( "Canonical tags is present on the page :" + URL);
}
else
{
Console.WriteLine( "Canonical tags is not present on the page :" + URL);
resultcount++;
}
return resultcount;
}

I have used System.Web.HttpUtility.UrlDecode because if I have to read the URL that has non English characters example Japanese the characters are encoded.

One thought on “Verifying the canonical tag on a web page using Selenium GetLocation

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>