Wednesday 8 February 2017

SELENIUM: How to get X,Y coordinates of WebElement in Selenium


How to get X,Y coordinates of WebElement in Selenium:



In any  software appliaction each and every web element has Its own position on page and generally It is measured in x and y pixels and known as x y coordinates of element. x pixels means horizontal position on page from left side and y pixels means vertical position on software web page from top side. You can find x y coordinates of any element

In selenium webdriver software testing tool, You can get x y coordinates of element using Point class.

Example:
package com.rameshsoft.rameshselenium;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class XYCoordinates {
                   public static void main(String[] args) throws Exception {
                             System.setProperty("webdriver.gecko.driver", "C:\\Users\\ramesh\\Desktop\\java\\desk\\geckodriver-v0.11.1-win64\\geckodriver.exe"); 
                             WebDriver driver=new FirefoxDriver();
                             System.out.println(driver.manage().window().getSize().height);
                             driver.manage().window().maximize();
                             driver.manage().timeouts().implicitlyWait(55,TimeUnit.SECONDS);
                             driver.manage().deleteAllCookies();                            
                   driver.navigate().to("https://www.gmail.com");
                   Thread.sleep(2000);                            
                             WebElement ele=driver.findElement(By.id("Email"));
                             Point p=ele.getLocation();
                             int xx=p.getX();
                   int yy=        p.getY();
                   System.out.println("X coordinate:  "+xx+" Y-Coordinate : "+yy);
          driver.quit();
}
}

No comments:

Post a Comment