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类找到了解决方案。

  1. 将Chrome切换为全屏:

    DesiredCapabilities dc = new DesiredCapabilities();
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--kiosk", "test-type");
    dc.setCapability(ChromeOptions.CAPABILITY, options);
    WebDriver driver = new ChromeDriver(dc);
    
  2. 获取起始元素和目标元素的坐标:

    // 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));
    
  3. 使用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);
    

相关文章