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拖放到滚动条
基础教程推荐
猜你喜欢
- 将 double 转换为 Int,向下舍入 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- JPA惰性列表上的流 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
