The getSize() method

The getSize method can also be called on all the visible components of HTML. It will return the width and height of the rendered WebElement. The API syntax of the getSize() method is as follows:

Dimension getSize()

The preceding method doesn't take any input parameters, and the return type is a class instance named Dimension. This class contains the width and height of the target WebElement. The following is the code to get the width and height of the Search box:

WebElement searchBox = driver.findElement(By.name("q"));System.out.println("Size of the box is: "        + searchBox.getSize());

The output for the preceding code is the width and height of the Search box, as shown in the following screenshot:

Size of the box is: (281, ...

Get Selenium WebDriver 3 Practical Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.