简体   繁体   English

如何从 selenium java 中的 getLocation 获得精确的 y 坐标?

[英]How to get exact y-coordinate from getLocation in selenium java?

Pre-req:前置要求:

Selenium 3.141 Selenium 3.141

Firefox browser Firefox浏览器

Requirement : Get x,y co-ordinates of an webelement and perform mouse move to xy co-ordinates.要求:获取网页元素的 x,y 坐标并执行鼠标移动到 xy 坐标。 X is calculated properly whereas y co-ordinate is falling 100 pixel short. X 计算正确,而y 坐标下降了 100 像素。

Note : Webelement Formfield is hidden, user will perform vertical scroll and click on it.注意:Webelement Formfield 是隐藏的,用户将执行垂直滚动并单击它。 Co-ordinates are taken after scrolling.滚动后获取坐标。

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)

Other trials:Tried with browser open in fullscreen mode but the same issue.其他试验:尝试以全屏模式打开浏览器,但同样的问题。

Queries:查询:

How to get exact y co-ordinate?如何获得精确的y坐标?

Is xy co-ordinate calculated from left top corner of desktop window or viewport? xy 坐标是从桌面左上角 window 还是视口计算的?


Update更新

Status:地位:

Based on your suggestion, I tried below line of code and yes, it automatically scrolls to the required element.根据您的建议,我尝试了下面的代码行,是的,它会自动滚动到所需的元素。

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

Outcome 1:结果1:

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

I get y co-ords as 700 but the element might be at 900 pixels.我得到 y co-ords 为 700,但元素可能为 900 像素。

When I do mousemove, it moves to 700 pixels as returned, which is not the element to be dragged.当我做 mousemove 时,它会移动到返回的 700 像素,这不是要拖动的元素。 My webelement (source) is still at 900 pixels.我的 webelement (source) 仍然是 900 像素。 X co-ord is completely OK. 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

or或者

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

Again y is falling short. y 再次不足。 What would be the reason?原因是什么?

Outcome 2: Above code snippet (movetoelement) works for chrome but not firefox.结果 2:上面的代码片段(movetoelement)适用于 chrome,但不适用于 firefox。

getLocation()获取位置()

getLocation() returns the point where on the page is the top left-hand corner of the rendered element is located ie a point, containing the location of the top left-hand corner of the element. getLocation()返回页面上渲染元素左上角所在的点,即一个点,包含元素左上角的位置。


As an example to extract the X and Y coordinates of the search box on Google Home Page you can use the following solution:作为提取Google 主页搜索框XY坐标的示例,您可以使用以下解决方案:

  • Code Block:代码块:

     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();
  • Console Output:控制台 Output:

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

Update 1更新 1

It is not that clear from your question why you want to perform mouse move to xy co-ordinates.从您的问题中不清楚为什么要将鼠标移动到xy坐标。 However for optimum results you need to:但是,为了获得最佳结果,您需要:


Conclusion结论

As you mentioned ...perform vertical scroll and click on it... , the scrollIntoView() looks as a pure overhead as:正如您所提到的...执行垂直滚动并单击它...scrollIntoView()看起来像一个纯粹的开销

the following methods:以下方法:

will automatically scroll the element within the Viewport .将自动滚动Viewport中的元素。


Update 2更新 2

As per your comment to address the error regarding ...out of bounds of viewport... you can refer the discussion org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (x, y) is out of bounds while MouseHover with GeckoDriver Firefox Selenium As per your comment to address the error regarding ...out of bounds of viewport... you can refer the discussion org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (x, y) is out of bounds while MouseHover with GeckoDriver Firefox Selenium


Update 3更新 3

As per your comment ...it moves to 700 pixels as returned, which is not the element to be dragged... it is worth mentioning that as I already mentioned within my answer that getLocation() returns the point where on the page is the top left-hand corner of the rendered element.根据您的评论...返回时它移动到 700 像素,这不是要拖动的元素...值得一提的是,正如我在回答中已经提到的那样getLocation()返回页面上的点渲染元素的左上角 Where as to perform a successful Drag and Drop operation, presumably the draggable element needs to be dragged up to certain percentage within the droppable element.而要执行成功的拖放操作,大概需要将可拖动元素拖动到可放置元素内的一定百分比。

Additionally, if the html5 element to be dragged have the attribute draggable it needs to be handled differently which is a different topic altogether and we can discuss it in a seperate thread.此外,如果要拖动的 html5 元素具有可draggable属性,则需要以不同方式处理它,这完全是一个不同的主题,我们可以在单独的线程中讨论它。

The relevant HTML would have helped us to a huge extent.相关的 HTML 会在很大程度上帮助我们。


References参考

You can find a couple of relevant detailed discussion in:您可以在以下位置找到一些相关的详细讨论:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM