简体   繁体   中英

Selenium WebDriver drag and drop to scrollbar

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.

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.

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

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

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