Saturday, January 23, 2016

How To Get X Y and Height Width Coordinates Of Element In Selenium WebDriver


public void getCoordinates() throws Exception {
 //Locate element for which you wants to retrieve x y coordinates.
    WebElement Image = driver.findElement(By.xpath("//img[@border='0']"));
    //Used points class to get x and y coordinates of element.
    Point point = Image.getLocation();
    int xcord = point.getX();
    System.out.println("Element's Position from left side Is "+xcord +" pixels.");
    int ycord = point.getY();
    System.out.println("Element's Position from top side Is "+ycord +" pixels.");
 }

public void getSize() throws Exception {
//Locate element for which you wants to get height and width.
    WebElement Image = driver.findElement(By.xpath("//img[@border='0']"));
    //Get width of element.
    int ImageWidth = Image.getSize().getWidth();
    System.out.println("Image width Is "+ImageWidth+" pixels");
    //Get height of element.
    int ImageHeight = Image.getSize().getHeight();
    System.out.println("Image height Is "+ImageHeight+" pixels");
 } 

No comments:

Post a Comment