Pre:
硒3.141
Firefox浏览器
Requirement:获取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像素,这不是要拖动的元素。我的资料(来源)仍然是900像素。X-命令是完全可以的。
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:上面的代码片段(movetoelement)中的适用于chrome,而不是firefox。
发布于 2019-10-24 11:16:09
getLocation()
getLocation()
返回页面上被呈现元素的左上角所在的点,即一个点,其中包含元素左上角的位置。
作为在谷歌主页上提取搜索框的X和Y坐标的示例,您可以使用以下解决方案:
更新1
从您的问题中还不清楚为什么要执行鼠标移动到xy
协调。但是,为了获得最佳结果,您需要:
visibilityOfElementLocated()
诱导scrollIntoView()
driver).executeScript("arguments.scrollIntoView(true);",新WebDriverWait(驱动程序,20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("element"))));)
-您可以在如何滚动到元素并单击selenium?中找到相关的讨论
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会在很大程度上帮助我们。
参考文献
您可以在以下几个方面找到相关的详细讨论:
https://stackoverflow.com/questions/58538692
复制相似问题