无法使用Java + Selenium WebDriver进行拖放

时间:2014-07-02 02:45:43

标签: java selenium-webdriver

以下是源元素的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>

以下是目标元素的HTML:

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

因此我:

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

Actions action = new Actions(driver);

为了实现拖放,我尝试了以下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();

我也试过moveToElement(target, x-offset, y-offset)仍然没有用。

当我尝试上述所有内容时,它没有抛出任何错误,下一个代码开始执行并且在视觉上,我可以看到源元素被拖动但看起来当它移动到目标元素时,它不会被丢弃到目标元素仍然没有错误。

实际上,如果Selenium WebDriver和浏览器版本兼容性存在问题,我原本期待一些错误。

如果有人知道解决方案或解决方法,请回答。

1 个答案:

答案 0 :(得分:0)

以下代码对我有用,试试这个:

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