简体   繁体   中英

Unable to drag and drop using Java + Selenium WebDriver

Following is HTML for source element:

<li draggable-effect-allowed="copy" draggable-data="business.domain.view.RecordViewComponentType" draggable="view-designer-drop-zone-content" class="list-group-item ng-scope ng-binding" ng-dblclick="insertComponentType(component)" ng-repeat="component in componentTypes" draggable="true">Record</li>

and following is HTML for target element:

<div droppable-on-drop="onDropComponent($event, $draggableEl, $droppableEl)" droppable="view-designer-drop-zone-content" class="layoutSection ContentAreaLayoutSection" id="CONTENT"></div>

Hence I've:

source = driver.findElement(By.xpath("//li[text()='Record']"));
target = driver.findElement(By.ID("CONTENT"));

Actions action = new Actions(driver);

In order to achieve drag and drop, I tried with following apis, but no luck:

1. action.dragAndDrop(source, target).build().perform();
2. action.clickAndHold(source).moveToElement(target).release(target).build().perform();
3. action.moveToElement(source).clickAndHold(source).moveToElement(target).release(target).build().perform();
4. action.moveToElement(source).clickAndHold().moveToElement(target).release().build().perform();
5. action.clickAndHold(source).moveToElement(target).build().perform();
   Thread.sleep(3000);
   action.release(target).build().perform();

I also tried with moveToElement(target, x-offset, y-offset) still didn't work.

When I tried with all of the above, it didn't throw any error and next code starts executing and visually, I can see source element is dragged but looks when it's moved to target element, it's not dropped to target element still no error is thrown.

Actually, I was expecting some error here if there is an issue with Selenium WebDriver and browser version compatibility.

If someone knows solution or workaround for this, please do answer.

The below code is working for me, try this:

 Actions act = new Actions(driver);
                    WebElement srcElement = driver.findElement(By
                            .id(locator)); 
                    Thread.sleep(3000);
                    WebElement targetElement =driver.findElement(By
                            .id(locator));
                    Thread.sleep(3000);
                    act.dragAndDrop(srcElement, targetElement);
                    Thread.sleep(3000);
                    act.build().perform();
                    Thread.sleep(3000);

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