How to convert xpath location to pixel

I'm wondering if it is possible to convert the xpath location or element id to pixel location (x, y) using java?

I have looked in various places but could not get a concise and simple answer.

+3


source to share


2 answers


Find the element and use getLocation()

method
:

WebElement element = driver.findElement(By.id("my_id")); 
System.out.println(element.getLocation());

      



Note that it getLocation()

returns "Point" (the coordinates of the top-left corner of the element).

+2


source


Below is the complete program.



  public void getCoordinates(){
  driver = new ChromeDriver();
  driver.get("https://www.google.com");
  WebElement element = driver.findElement(By.id("hplogo"));

  Point location = element.getLocation();
  int x = location.getX();
  int y = location.getY();
  System.out.println("Coordinates of an element is : " + x + " and " + y);
 }

      

+2


source







All Articles