Tuesday 7 November 2017

Identifying location and size of the object

Identifying location and size of the object
In some Cases you need to find where the object exactly located in the webpage(x coordinate and Y- coordinate) and Size(width and height)
Use the below code to get the objects(Elements) location and size
In this example i am writing code to identify the Google Page Search Box location and  Size
First Identify the WebElement:
WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
//First identify the WebElement to which you want the location and size
WebElement ele=driver.findElement(By.name("q"));
Location:
ele.getLocation(); method should be used to get the location of the object
The return type of the getLocation() method is “Point” class instance
After creating object to getLocation object we can use getx() and getY() method to get x-coordinate and Y-coordinate
Return Type of getX() and getY() is integer.
Point point=ele.getLocation();
int x=point.getX();
int y=point.getY();
System.out.println(x);
System.out.println(y);
Size:
ele.size(); method should be used to get the Size of the object
The return type of the getSize() method is “Dimension” class instance
After creating object to getLocation object we can use getHeight() and getWidth() method to get Height and Width respectively.
Dimension dim=ele.getSize();
dim.getHeight();
dim.getWidth();
                               
System.out.println(dim.getHeight());

System.out.println(dim.getWidth());

0 comments:

Post a Comment