Selenium WebDriver drag and drop to scrollbar(Selenium WebDriver拖放到滚动条)
本文介绍了Selenium WebDriver拖放到滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对Selenium WebDriver拖放有问题。它不想掉到滚动条上。 我尝试过这个:
new Actions(SeleniumDriver.getDriver())).dragAndDrop(element, target).build().perform();
还尝试使用偏移量:
(new Actions(SeleniumDriver.getDriver()))
.dragAndDropBy(element, xoffset, yoffset).build().perform();
并尝试使用:
Actions builder = new Actions(SeleniumDriver.getDriver());
builder.clickAndHold(element).build().perform();
builder.moveToElement(target).build().perform();
builder.release(target).build().perform();
有人知道滚动条的工作解决方案吗?感谢您的帮助。
推荐答案
我使用Java Robot类找到了解决方案。
将Chrome切换为全屏:
DesiredCapabilities dc = new DesiredCapabilities(); ChromeOptions options = new ChromeOptions(); options.addArguments("--kiosk", "test-type"); dc.setCapability(ChromeOptions.CAPABILITY, options); WebDriver driver = new ChromeDriver(dc);
获取起始元素和目标元素的坐标:
// start coordinates int startX = new Integer(element.getLocation().x); int startY = new Integer(element.getLocation().y); // destination dimensions int startWidth = new Integer(element.getSize().width); int startHeight = new Integer(element.getSize().height); // destination coordinates int destinationX = new Integer(target.getLocation().x); int destinationY = new Integer(target.getLocation().y); // destination dimensions int destinationWidth = new Integer(target.getSize().width); int destinationHeight = new Integer(target.getSize().height); // work out destination coordinates int endX = Math.round(destinationX + (destinationWidth / 2)); int endY = Math.round(destinationY + (destinationHeight / 2)); int sX = Math.round(startX + (startWidth / 2)); int sY = Math.round(startY + (startHeight / 2));
使用Java Robot类进行拖放:
Thread.sleep(1000); Robot robot = new Robot(); robot.mouseMove(sX, sY); robot.mousePress(InputEvent.BUTTON1_MASK); robot.mouseMove(endX, endY); robot.mouseRelease(InputEvent.BUTTON1_MASK);
这篇关于Selenium WebDriver拖放到滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:Selenium WebDriver拖放到滚动条


猜你喜欢
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 转换 ldap 日期 2022-01-01
- 获取数字的最后一位 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01