Java Selenium WebDriver拖放到滚动条

Java Selenium WebDriver拖放到滚动条,java,selenium,drag-and-drop,selenium-webdriver,scrollbar,Java,Selenium,Drag And Drop,Selenium Webdriver,Scrollbar,我对SeleniumWebDriver的拖放操作有异议。它不想在滚动条中删除webelement。 我试过这个: new Actions(SeleniumDriver.getDriver())).dragAndDrop(element, target).build().perform(); 还尝试使用偏移: (new Actions(SeleniumDriver.getDriver())) .dragAndDropBy(element, xoffset, yoffset)

我对SeleniumWebDriver的拖放操作有异议。它不想在滚动条中删除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();

有人知道滚动条的工作解决方案吗?感谢您的帮助。

请确保您正在尝试拖动正确的元素。 也许那个元素不是可拖动的,它可能是一个可拖动的内部元素。

一些想法

  • 点击并按住按钮后,需要小睡一会。我已经测试了一些应用程序,它们需要100毫秒的等待时间,直到拖放初始化

  • 它可能是使用HTML5拖放,而我上次尝试使用的是Selenium不支持的

  • user3723314建议的元素错误


我通过使用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);
    

  • 我拖着我试着睡觉。这没用。我的项目使用HTML4。我注意到当selenium想用滚动条插入aria时,底部滚动条会移动。但元素并没有下降。