无法执行拖放操作,即使执行代码也没有任何错误

时间:2016-09-28 07:55:43

标签: java selenium-webdriver

即使执行拖放操作没有任何错误,也无法在AnjularJS应用程序中看到元素被拖动。

以下是代码:

WebElement draggable = driver.findElement(By.xpath("//*[@id='page-content-wrapper']/div[2]/div[3]/div[4]/div/div[2]/div/div/div[2]/div/div[1]/ul/li/ul/li[2]/span/span[1]"));
WebElement droppable = driver.findElement(By.xpath(".//*[@id='page-content-wrapper']/div[2]/div[3]/div[3]/div[2]/div[1]/div[1]/div/div[6]/div[2]/div[1]"));

以下是执行和执行的不同方法,没有错误:

1)
Actions builder = new Actions(driver);
        builder.keyDown(Keys.CONTROL).click(draggable).click(droppable). keyUp(Keys.CONTROL).build().perform();

2)Method 2
Actions builder = new Actions(driver);
            builder.clickAndHold(draggable).moveToElement(droppable).build();
            builder.dragAndDrop(draggable, droppable).perform();

3)Method 3
Actions action = new Actions(driver); 
            Thread.sleep(1000);
            action.clickAndHold(draggable2).build().perform();
            action.moveToElement(droppable2).build().perform();
            action.release(droppable2).build().perform();

4)
Method 4
Actions actions = new Actions(driver); 
        browser.actions()
        .mouseDown(draggable2)
        .mouseMove(draggable2)
        .mouseUp(droppable2)
        .perform();

还执行了JavaScript方法:但没有运气它也可以无误地执行但无法在UI enter image description here中执行拖放操作

    A) public static void DragAndDropJS2(WebElement source, WebElement destination, WebDriver driver) throws Exception 
    {
       // JavascriptExecutor js = (JavascriptExecutor) driver;
        String xto=Integer.toString(source.getLocation().x);
        String yto=Integer.toString(destination.getLocation().y);
        ((JavascriptExecutor)driver).executeScript("function simulate(f,c,d,e){var b,a=null;for(b in eventMatchers)if(eventMatchers[b].test(c)){a=b;break}if(!a)return!1;document.createEvent?(b=document.createEvent(a),a==\"HTMLEvents\"?b.initEvent(c,!0,!0):b.initMouseEvent(c,!0,!0,document.defaultView,0,d,e,d,e,!1,!1,!1,!1,0,null),f.dispatchEvent(b)):(a=document.createEventObject(),a.detail=0,a.screenX=d,a.screenY=e,a.clientX=d,a.clientY=e,a.ctrlKey=!1,a.altKey=!1,a.shiftKey=!1,a.metaKey=!1,a.button=1,f.fireEvent(\"on\"+c,a));return!0} var eventMatchers={HTMLEvents:/^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,MouseEvents:/^(?:click|dblclick|mouse(?:down|up|over|move|out))$/}; " +
                "simulate(arguments[0],\"mousedown\",0,0); simulate(arguments[0],\"mousemove\",arguments[1],arguments[2]); simulate(arguments[0],\"mouseup\",arguments[1],arguments[2]); ",
                source,xto,yto);

    }



    B) public static void DragAndDropJS(WebElement source, WebElement destination, WebDriver driver) throws Exception 
    {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        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);",source, destination);
        Thread.sleep(1500);

    }



    c) public static void DragAndDropJS1(WebElement source, WebElement destination, WebDriver driver) throws Exception 
    {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        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" +"}\n" +"\n" +"var source = arguments[0];\n" +"var destination = arguments[1];\n" +"simulateHTML5DragAndDrop(source,destination);",source, destination);
        Thread.sleep(1500);   
    }

所有上述方法和命令都经过编译和执行,没有任何错误,但无法在UI中执行拖放操作(PLEASE HELP)。

尝试了所有可能的组合,我知道任何人都可以让我知道我错过了什么吗? 仅供参考我使用Chrome 53和Firefox 42会有什么不同吗? 还有其他工具可以满足我的要求吗?

1 个答案:

答案 0 :(得分:0)

只需确保网页中的元素不是HTML5网络元素。因为selenium不支持对HTML5元素执行操作。

请查看http://elementalselenium.com/tips/39-drag-and-drop,了解如何使用javascript对HTML5元素执行拖放操作。

此主题也可以帮助您 - Error when trying to execute drag and drop using javascript for Selenium WebDriver test