How to verify the image height and width in Selenium

Its now easy to verify the image height and width of an image on a web page using Selenium. Here is the code that verifies if the height of the image is between 595 and 686 px and width is 230 px.

decimal height = selenium.GetElementHeight("//img[@class='heroImageMain']");
decimal width = selenium.GetElementWidth("//img[@class='heroImageMain']");
Console.WriteLine("Hero Image dimensions:- " + width + " x " + height + " pixels.");
if ((height >= 595 && height <= 686) && (width == 230))
{
console.writeline(Testcase + "Hero image dimensions are invalid -> " + +width + " x " + height + " pixels.");
}

Submitted by: Horgan Barry

How did this solution work for you? Do you have a better alternative? Feel free to share your thoughts in comments section below.

2 thoughts on “How to verify the image height and width in Selenium

    • Pavandeep Puddupakkam says:

      You can create a function/method and the parse the xpath of the images. You can modify the function to parse the height and width
      public void ImageSizeVerification(string xpath)
      {
      decimal height = selenium.GetElementHeight(xpath);
      decimal width = selenium.GetElementWidth(xpath);
      Console.WriteLine(“Image dimensions:- ” + width + ” x ” + height + ” pixels.”);
      if ((height >= 595 && height < = 686) && (width == 230))
      {
      Console.WriteLine(Testcase + "Image dimensions are invalid -> ” + +width + ” x ” + height + ” pixels.”);
      }
      }

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>