【问题标题】:How to convert xpath location to pixel如何将xpath位置转换为像素
【发布时间】:2015-03-11 16:29:39
【问题描述】:

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

我在各个地方都看过,但无法得到一个简洁明了的答案。

【问题讨论】:

    标签: java html selenium xpath selenium-webdriver


    【解决方案1】:

    下面是完整的程序。

      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】:

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

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

      请注意,getLocation() 返回一个“点”(元素左上角的坐标)。

      【讨论】:

      • 如何在方法中使用这些像素?例如打印输出 217,1045 我想在使用拖放机器人的方法中使用这些坐标。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      • 2013-02-02
      • 2011-01-01
      • 1970-01-01
      相关资源
      最近更新 更多