我对 Selenium WebDriver 拖放有疑问。它不想拖放到滚动条中的 webelement。
我试过这个:
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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)