简体   繁体   English

无法使用Java + Selenium WebDriver拖放

[英]Unable to drag and drop using Java + Selenium WebDriver

Following is HTML for source element: 以下是源元素的HTML:

<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: 以下是目标元素的HTML:

<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: 为了实现拖放,我尝试使用以下API,但没有运气:

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. 我也尝试过moveToElement(target, x-offset, y-offset)仍然不起作用。

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. 实际上,如果Selenium WebDriver和浏览器版本兼容性存在问题,我期望这里出现一些错误。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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