如何将xpath位置转换为像素

How to convert xpath location to pixel

我想知道是否可以使用 java 将 xpath 位置或元素 ID 转换为像素位置 (x,y)?

我查看了多个位置,但无法得到简明直接的答案。

找到元素并使用 getLocation() method:

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

注意 getLocation() returns a "Point"(元素左上角的坐标)。

下面是完整的程序。

  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);
 }