【问题标题】:Selenium WebDriver drag and drop to scrollbarSelenium WebDriver 拖放到滚动条
【发布时间】:2014-06-13 06:20:25
【问题描述】:

我对 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();

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

【问题讨论】:

  • 我注意到当 selenium 想要在带有滚动条的咏叹调中下降时,底部滚动条会移动。但是元素没有掉线。

标签: java selenium drag-and-drop selenium-webdriver scrollbar


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我拖动
    【解决方案3】:

    一些想法;

    • 单击并按住后需要短暂睡眠。我已经测试了在拖放我们初始化之前需要 100 毫秒保持的应用程序。

    • 可能是使用 HTML 5 拖放,我上次尝试的 Selenium 不支持

    • user3723314 建议的元素错误

    【讨论】:

    • 我试过睡觉。它没有帮助。我的项目使用 HTML 4。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    • 2023-03-17
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    相关资源
    最近更新 更多