Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
拖放;在java中使用SeleniumWebDriver删除分层列表_Java_Selenium Webdriver_Drag And Drop - Fatal编程技术网

拖放;在java中使用SeleniumWebDriver删除分层列表

拖放;在java中使用SeleniumWebDriver删除分层列表,java,selenium-webdriver,drag-and-drop,Java,Selenium Webdriver,Drag And Drop,在java中使用SeleniumWebDriver拖放分层列表 Github link: https://github.com/dbushell/Nestable Demo link:- http://dbushell.github.io/Nestable/ 我想在java中使用SeleniumWebDriver拖放分层列表。有关更多详细信息,请参阅演示链接 我试过以下方法:- 获取源元素 使用class=dd占位符创建一个div 拖动源元素并拖放到新创建的div元素上 错误:-无法在新创建

在java中使用SeleniumWebDriver拖放分层列表

Github link: https://github.com/dbushell/Nestable

Demo link:- http://dbushell.github.io/Nestable/
我想在java中使用SeleniumWebDriver拖放分层列表。有关更多详细信息,请参阅演示链接

我试过以下方法:-

  • 获取源元素
  • 使用class=dd占位符创建一个div
  • 拖动源元素并拖放到新创建的div元素上
  • 错误:-无法在新创建的div元素上删除源元素

    我该怎么做呢。是否可以使用SeleniumWebDriver

    Note: See demo link
    

    我发现拖放功能根据您使用的浏览器有所不同,我必须在拖放位置添加一个偏移量,以使其在Chrome Firefox和IE中保持一致

    这是一个C#代码段,但java应该类似。这个实例中的目标是我用JS插入的占位符div,就像您的方法一样

    Actions act = new Actions(WebDriver);
    act.ClickAndHold(source);
    act.MoveToElement(target);
    act.MoveByOffset(0, 5); //Minimum 1 pixel offset for Chrome, 5 for IE 
    act.Release(source);
    act.Build().Perform();
    

    此方法使用Javascript将webelement从一个位置拖放到另一个位置。不要被它的javascript困住:)。只需通过提供from和to参数来使用该方法。祝你好运

    public void dragAndDrop(WebElement from, WebElement to) {
        js.executeScript("function createEvent(typeOfEvent) {\n" + "var event =document.createEvent(\"CustomEvent\");\n"
                + "event.initCustomEvent(typeOfEvent,true, true, null);\n" + "event.dataTransfer = {\n" + "data: {},\n"
                + "setData: function (key, value) {\n" + "this.data[key] = value;\n" + "},\n"
                + "getData: function (key) {\n" + "return this.data[key];\n" + "}\n" + "};\n" + "return event;\n"
                + "}\n" + "\n" + "function dispatchEvent(element, event,transferData) {\n"
                + "if (transferData !== undefined) {\n" + "event.dataTransfer = transferData;\n" + "}\n"
                + "if (element.dispatchEvent) {\n" + "element.dispatchEvent(event);\n"
                + "} else if (element.fireEvent) {\n" + "element.fireEvent(\"on\" + event.type, event);\n" + "}\n"
                + "}\n" + "\n" + "function simulateHTML5DragAndDrop(element, destination) {\n"
                + "var dragStartEvent =createEvent('dragstart');\n" + "dispatchEvent(element, dragStartEvent);\n"
                + "var dropEvent = createEvent('drop');\n"
                + "dispatchEvent(destination, dropEvent,dragStartEvent.dataTransfer);\n"
                + "var dragEndEvent = createEvent('dragend');\n"
                + "dispatchEvent(element, dragEndEvent,dropEvent.dataTransfer);\n" + "}\n" + "\n"
                + "var source = arguments[0];\n" + "var destination = arguments[1];\n"
                + "simulateHTML5DragAndDrop(source,destination);", from, to);
    
    }
    

    dragAndDrop在源为div元素而目标为li标记的情况下不起作用吗?@VivekSingh请正确查看演示。首先,div[class='dd-placeholder']将在drop之前创建,drop之后仅创建li标记。如果正确,您需要(例如)将项目6移动到项目1?是吗?@VivekSingh我需要移动元素。如何将源元素放到class=dd占位符的新创建的div中,因为移动到的div元素包含在li元素中。将目标设为li not div。我尝试过(如果将项6移动到项1)--
    source=driver.findElement(By.xpath(“//div[.='item 6']);target=driver.findElement(By.xpath(//li[@data id='1'])我使用的代码与您遵循的代码相同。但是,您可以使用演示链接中提到的selenium来模拟功能吗。我做不到。