Saturday, 27 April 2013


Getting the width and height of an Image using Selenium

People has kept asking me how to capture the width and height of an image/element using selenium. I tried to get an answer to it and the solution is very simple. The solution comes with Selenium itself.

Selenium-1 consists of certain functions for storing element values like getElementHeight, getElementWidth, getElementPositionLeft, getElementPositionRight, getElementPositionTop.

Similarly in Selenium-2(WebDriver) you can use the getSize() function which will return a "Dimension" class object. You can then use the getHeight() and getWidth() functions over it for getting the width and height of the element. 


We can use these functions to get the position of the element and also its width and height.
Following is an example for getting the height and width of an image/element on the browser using Selenium-1.

?
1
2
height = selenium.getElementHeight("location to the image on the browser.(xpath or id)")
width = selenium.getElementWidth("location to the image on the browser.(xpath or id)")

Following is an example for getting the height and width of an image/element on the browser using Selenium-2(WebDriver).
?
1
2
height = driver.findElement("location to the image on the browser.(xpath or id)").getSize().getHeight();
width = driver.findElement("location to the image on the browser.(xpath or id)").getSize().getWidth()

1 comment:

  1. The getSize() method in Selenium WebDriver is indeed elegant and gives you direct access to an element’s rendered dimensions via the Dimension class — something that’s incredibly useful for responsive design testing or visual layout validation.

    If you’re building out a full automation suite around these checks, it’s worth pairing your Selenium scripts with a strong reporting and test tracking platform. A modern test management software like Tuskr lets you consolidate test cases, results, and metrics in one place, so visual verification tests like this can be reviewed, compared, and refined across releases with clarity and accountability.

    ReplyDelete

Angular JS Protractor Installation process - Tutorial Part 1

                     Protractor, formally known as E2E testing framework, is an open source functional automation framework designed spe...