Selenium dragAndDrop拖动到物理光标

时间:2019-05-25 11:35:54

标签: selenium

硒-3.141.59
Java

在执行dragAndDrop方法时,源元素不是移动到目标元素,而是物理光标。

尝试以下操作:

public static void dragAndDrop(WebDriver driver, WebElement src, WebElement target) {
    Actions builder = new Actions(driver);

    builder.moveToElement(target).build().perform();

    Action dragAndDrop = builder.clickAndHold(src)
            .moveToElement(target)
            .release(target)
            .build();
    dragAndDrop.perform();
}

然后是

public static void dragAndDrop(WebDriver driver, WebElement src, WebElement target) {
    Actions builder = new Actions(driver);
    builder.dragAndDrop(src, target).build().perform();
}

但是这些解决方案不起作用。

只有此解决方案有效:

public static void dragAndDropRobot(WebDriver driver, WebElement src, WebElement target) throws InterruptedException {
    Robot robot;
    int xWindow = driver.manage().window().getPosition().x;
    int yWindow = driver.manage().window().getPosition().y;
    int xTarget = target.getLocation().x;
    int yTarget = target.getLocation().y;

    Actions act = new Actions(driver);
    act.dragAndDrop(src, target).build().perform();
    try {
        robot = new Robot();
        robot.mouseMove((xWindow + xTarget), (yWindow + yTarget));
        Thread.sleep(2000);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
    } catch (AWTException e) {
        e.printStackTrace();
    }
}

除了机器人以外,还有什么解决方案可以解决此问题?

0 个答案:

没有答案