Selenium WebDriver拖放到滚动条 [英] Selenium WebDriver drag and drop to scrollbar

查看:49
本文介绍了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);
    

这篇关于Selenium WebDriver拖放到滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆