简体   繁体   中英

Selenium WebDriver doesn't press key while drag & drop

I have to test a scenario where a simple drag and drop moves the element and a drag and drop with CTRL key pressed copies the element.

The correct order in which copy works manually tested is:

  1. hold element
  2. move to target element
  3. press CTRL
  4. release element at target

This is my code:

        Actions builder = new Actions(driver);

        Action dragAndDrop = builder.clickAndHold(element)
                .moveToElement(target).keyDown(Keys.CONTROL)
                .release().build();

        dragAndDrop.perform();

The thing is, the result is a move instead of a copy. So I guess it's ignoring the key press.

Can you tell me what I'm doing wrong? Thank you

I solved my issue using robot. Here is code example:

        Actions builder = new Actions(driver);
        Robot robot = new Robot();
        robot.setAutoDelay(500);

        Action drag = builder.clickAndHold(element).moveToElement(target)
                .build();

        drag.perform();

        robot.keyPress(KeyEvent.VK_CONTROL);

        Action drop = builder.release().build();
        drop.perform();
        robot.keyRelease(KeyEvent.VK_CONTROL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM