首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在selenium中从getLocation中获得精确的y坐标?

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

Stack Overflow用户
提问于 2019-10-24 09:49:40
回答 1查看 3.6K关注 0票数 2

Pre:

硒3.141

Firefox浏览器

Requirement:获取x,y坐标,并执行鼠标移动到xy坐标.X是正确计算的,而y坐标下降了100像素。

备注:Webelement Formfield是隐藏的,用户将执行垂直滚动并单击它。协调是在滚动后进行的。

代码语言:javascript
运行
复制
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是从桌面窗口或视图的左上角计算的吗?

更新

状态:

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

代码语言:javascript
运行
复制
WebElement source = fromfield.findElement(By.xpath(".//*[contains(@title,'test')]"));
 new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(source))).build().perform();

结果1:

代码语言:javascript
运行
复制
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像素,这不是要拖动的元素。我的资料(来源)仍然是900像素。X-命令是完全可以的。

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(source))).clickAndHold(source).build().perform();

又一次,Y不够格了。原因是什么?

结果2:上面的代码片段(movetoelement)中的适用于chrome,而不是firefox。

EN

回答 1

Stack Overflow用户

发布于 2019-10-24 11:16:09

getLocation()

getLocation()返回页面上被呈现元素的左上角所在的点,即一个点,其中包含元素左上角的位置。

作为在谷歌主页上提取搜索框的X和Y坐标的示例,您可以使用以下解决方案:

  • 代码块: 10).until(ExpectedConditions.elementToBeClickable(By.name("q")));Point elementLocation = element.getLocation();System.out.println(元素的X坐标为:“+elementLocation.getX()”;System.out.println(元素的Y坐标:“+elementLocation.getY()”;/或WebElement elem =新WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.name("q")));))System.out.println("X坐标是:"+elem.getLocation().getX());System.out.println("Y坐标是:“+elem.getLocation().getY();driver.quit();
  • 控制台输出: 元素的X坐标是:元素的439 Y坐标: 322 X坐标是: 439 Y坐标是: 322

更新1

从您的问题中还不清楚为什么要执行鼠标移动到xy协调。但是,为了获得最佳结果,您需要:

  • 在尝试调用WebDriverWait之前为visibilityOfElementLocated()诱导scrollIntoView()

driver).executeScript("arguments.scrollIntoView(true);",新WebDriverWait(驱动程序,20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("element"))));)

-您可以在如何滚动到元素并单击selenium?中找到相关的讨论

  • 在尝试交互(即调用WebDriverWait )之前为elementToBeClickable()诱导click()

20).until(ExpectedConditions.visibilityOf(ParentElement)))).perform();新动作(驱动程序).moveToElement(新WebDriverWait(驱动程序,新WebDriverWait)(驱动程序,20).until(ExpectedConditions.elementToBeClickable(By.xpath(subElement))).click();)

-您可以在如何鼠标鼠标悬停父元素,然后使用Selenium和Action类单击子元素中找到相关的讨论

结论

正如您提到的...perform垂直滚动并单击它.,scrollIntoView()看起来像一个纯开销,如下所示:

以下方法:

将自动滚动视图端口中的元素

更新2

根据您的评论,以解决有关视口边界的...out错误.您可以参考讨论org.openqa.selenium.interactions.MoveTargetOutOfBoundsException:(x,y)是越界的,而MouseHover则是GeckoDriver Firefox Selenium

更新3

根据您的注释,...it移动到700像素作为返回,这不是要拖动的元素.值得一提的是,正如我在回答中提到的那样,getLocation()返回页面上呈现元素的左上角的点。在执行成功的拖放操作时,可能需要将可拖放元素拖到可下垂元素中的某个百分比。

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

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

参考文献

您可以在以下几个方面找到相关的详细讨论:

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58538692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档