How to get Height and Width of an image or element using Selenium Webdriver


Selenium webdriver script to get Width and Height of an Image

Script1:-

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Sample {

public static void main(String[] args)
{


WebDriver driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com/");
System.out.println("Height of Mercury Tours image= "+ driver.findElement(By.xpath("html/body/div[1]/table/tbody/tr/td[1]/table/tbody/tr/td/table/tbody/tr/td/p[1]/img")).getSize().getHeight());
System.out.println("Width of Mercury Tours image= "+ driver.findElement(By.xpath("html/body/div[1]/table/tbody/tr/td[1]/table/tbody/tr/td/table/tbody/tr/td/p[1]/img")).getSize().getWidth());

}
}


Output:-

Height of Mercury Tours image= 110
Width of Mercury Tours image= 100


element height and width in webdriver


Script2:-

Script to get Height and Width of google search button

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Sample {

public static void main(String[] args)
{


WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
System.out.println("Height= "+ driver.findElement(By.name("btnK")).getSize().getHeight());
System.out.println("Width= "+ driver.findElement(By.name("btnK")).getSize().getWidth());

}
}


Output:-

Height= 29
Width= 102


No comments:

Related Posts Plugin for WordPress, Blogger...

Fun and Knowledge