【问题标题】:How to get exact y-coordinate from getLocation in selenium java?如何从 selenium java 中的 getLocation 获取精确的 y 坐标?
【发布时间】:2020-02-20 15:15:37
【问题描述】:

前置要求:

硒 3.141

火狐浏览器

要求:获取网页元素的 x,y 坐标并执行鼠标移动到 xy 坐标。 X 计算正确,而 y 坐标下降了 100 像素。

注意:Webelement Formfield是隐藏的,用户将执行垂直滚动并点击它。滚动后获取坐标。

WebElement fromfield = driver.findElement(By.xpath("//*[contains(@data-field-name,'deliverables')]"));
jse.executeScript("arguments[0].scrollIntoView();",fromfield); //scroll to the webelement, a small wait is given after scrolling

org.openqa.selenium.Point fromLocation = fromfield.getLocation();

int fromfield_x = fromLocation.x; 
int fromfield_y = fromLocation.y; //getx/gety returns same values

Actual Output: x = 550, y = 600
Expected Output: x = 550, y = 700. (**Note**: If I pass 700, then mouse moves correctly to required element, but here y is calculated incorrect)

其他试验:尝试以全屏模式打开浏览器,但同样的问题。

查询:

如何获得精确的 y 坐标?

xy 坐标是从桌面窗口或视口的左上角计算出来的吗?


更新

状态:

根据您的建议,我尝试了下面的代码行,是的,它会自动滚动到所需的元素。

WebElement source = fromfield.findElement(By.xpath(".//*[contains(@title,'test')]"));
 new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(source))).build().perform();

结果 1:

System.out.println("Y coordinate is: "+source.getLocation().getY());
int from_x = source.getLocation().getX();
int from_y = source.getLocation().getY();

我将 y 坐标设为 700,但元素可能为 900 像素。

当我做 mousemove 时,它​​移动到返回的 700 像素,这不是要拖动的元素。我的 webelement (source) 仍然是 900 像素。 X坐标完全OK。

robot.mouseMove(from_x , from_y); //moves to 700 pixels
Actions maction=new Actions(driver);
Action drag = maction.clickAndHold(source).pause(3000).build();
drag.perform(); //tries to drag from 700 pixels

new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(source))).clickAndHold(source).build().perform();

y 再次不足。原因是什么?

结果 2: 以上代码 sn-p (movetoelement) 适用于 chrome 但不适用于 firefox。

【问题讨论】:

  • 当你点击那些“错误”的坐标时会发生什么?什么都没有或有什么异常被抛出?
  • @AlexeyR. 没有例外。鼠标在错误的坐标处执行点击并继续下一个测试用例。

标签: java selenium webdriver selenium-chromedriver getlocation


【解决方案1】:

getLocation()

getLocation() 返回页面上渲染元素的左上角所在的点,即一个点,包含元素左上角的位置.


以提取Google Home Page搜索框XY坐标为例,您可以使用以下解决方案:

  • 代码块:

    driver.get("https://www.google.com/");
    WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.name("q")));
    Point elementLocation = element.getLocation();
    System.out.println("X coordinate of the element is: "+elementLocation.getX());
    System.out.println("Y coordinate of the element : "+elementLocation.getY());
    //or
    WebElement elem = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.name("q")));
    System.out.println("X coordinate is: "+elem.getLocation().getX());
    System.out.println("Y coordinate is: "+elem.getLocation().getY());
    driver.quit();
    
  • 控制台输出:

    X coordinate of the element is: 439
    Y coordinate of the element : 322
    X coordinate is: 439
    Y coordinate is: 322
    

更新 1

您的问题并不清楚为什么要将鼠标移动到xy 坐标。但是,为了获得最佳结果,您需要:

  • 在尝试调用 scrollIntoView() 之前,为 visibilityOfElementLocated() 引入 WebDriverWait

    • 举个例子:

      ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("element"))));
      
    • 可以在How to scroll UP to an element and click in selenium?找到相关讨论

  • 在您尝试交互之前为elementToBeClickable() 引入WebDriverWait,即调用click()


结论

正如您提到的...执行垂直滚动并单击它...scrollIntoView() 看起来像一个纯粹的开销

以下方法:

会自动滚动视口中的元素。


更新 2

根据您的评论来解决有关 ...超出视口范围... 您可以参考讨论 org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (x, y) is out of bounds while MouseHover with GeckoDriver Firefox Selenium


更新 3

根据您的评论 ...返回时移动到 700 像素,这不是要拖动的元素...值得一提的是,正如我在回答中已经提到的那样 getLocation() 返回页面上呈现元素左上角的点。至于要成功执行拖放操作,想必可拖动元素需要在可放置元素内拖动到一定百分比。

另外,如果要拖动的html5元素具有draggable属性,则需要进行不同的处理,这是一个完全不同的主题,我们可以在单独的线程中讨论。

相关的 HTML 会在很大程度上帮助我们。


参考文献

您可以在以下位置找到一些相关的详细讨论:

【讨论】:

  • 谢谢。我能够得到 x 和 y 坐标,但 y 坐标不正确。它短 100 像素。如何获得精确的 y 坐标?
  • @sridattas 查看更新的答案并让我知道状态。
  • @DebanjanB。再次感谢。我尝试按照建议将 scrollintoview 替换为 movetoelement 和 elementtobeclickable ,但出现异常:(517、853.25)超出视口宽度(1898)和高度(829)的范围,没有滚动发生。用例是执行元素的拖放并且元素被隐藏,因此我需要滚动。我尝试了所有其他的拖放操作,但由于它是 html5 拖放操作,所以没有工作。我还尝试了 rcorreia 提供的解决方案。现在我正在尝试使用与硬编码 xy 坐标一起使用的 mousemove/press/release。但是,我尝试动态传递坐标,x 可以,但 y 不足。
  • html5 dragdrop 是这里的关键考虑因素,您可能想将其作为一个单独的问题提出,因为这将是完全不同的故事。
  • @sridattas 查看更新的答案,让我知道状态。
猜你喜欢
  • 2011-06-20
  • 2014-06-15
  • 1970-01-01
  • 1970-01-01
  • 2016-06-28
  • 2022-11-13
  • 2022-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多