2

I have issue with Selenium WebDriver drag-and-drop. It didn't want drop to webelement in scroll-bar. I tried this:

new Actions(SeleniumDriver.getDriver())).dragAndDrop(element, target).build().perform();

also tried by using offsets:

(new Actions(SeleniumDriver.getDriver()))
           .dragAndDropBy(element, xoffset, yoffset).build().perform();

and try use:

Actions builder = new Actions(SeleniumDriver.getDriver());
builder.clickAndHold(element).build().perform();
builder.moveToElement(target).build().perform();
builder.release(target).build().perform();

Any body know working solutions for scroll-bars? Thank's for any help.

1
  • I noticed what bottom scroll-bar moves when selenium want drop in aria with scroll-bars. But element didn't dropped. Jun 13, 2014 at 11:17

3 Answers 3

2

I found solution by using Java Robot class.

  1. Switch chrome to full screen:

    DesiredCapabilities dc = new DesiredCapabilities();
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--kiosk", "test-type");
    dc.setCapability(ChromeOptions.CAPABILITY, options);
    WebDriver driver = new ChromeDriver(dc);
    
  2. Get coordinates of start and destination element:

    // 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. Use Java Robot class for drag and drop:

    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);
    
1

Make sure you are trying to drag the right element. Maybe that element is not dragable, it could be an inner element that is dragable.

1
  • I drag <img draggable="true" src="somelink"> Jun 13, 2014 at 11:05
0

A few ideas;

  • A small sleep is required after the click and hold. I have tested applications which required 100ms of hold until the drag and drop us initialized.

  • It could be using HTML 5 drag and drop which last I tried wasn't supported by Selenium

  • wrong element as suggested by user3723314

1
  • I tried sleep. It didn't help. My project use HTML 4. Jun 13, 2014 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.