如何在Selenium Java中的特定位置拖放?

时间:2018-07-12 12:56:06

标签: java selenium

我正在尝试将2个元素拖放到我的工作空间中,但是它们是相互放置的,我如何指定放置元素的位置?

我正在使用dragAndDrop()函数

Actions act=new Actions(driver);
act.dragAndDrop(From, To).build().perform(); 

enter image description here

2 个答案:

答案 0 :(得分:0)

使用dragAndDrop()方法,一次只能在一个位置上执行拖放操作。它通常用于对可删除的Web元素执行操作。请找到各种类型的可放置元素的参考文献http://demoqa.com/droppable/

要实现拖放操作-

示例:

 WebElement From=driver.findElement(By.xpath( <<source xpath>> ));
 WebElement To=driver.findElement(By.xpath( <<destination xpath>> ));
 Actions actions=new Actions(driver);
 actions.dragAndDrop(From, To).build().perform(); 

要拖放另一个元素,您需要再次首先执行所有步骤。

希望这会有所帮助:)

答案 1 :(得分:0)

尝试使用以下代码:

 public static  void dragAndDropViaJQueryHelper(WebDriver driver, String dragSourceJQuerySelector, String dropTargetJQuerySelector) {
        String jqueryScript = "(function( jquery ) {\r\n" + 
                "        jquery.fn.simulateDragDrop = function(options) {\r\n" + 
                "                return this.each(function() {\r\n" + 
                "                        new jquery.simulateDragDrop(this, options);\r\n" + 
                "                });\r\n" + 
                "        };\r\n" + 
                "        jquery.simulateDragDrop = function(elem, options) {\r\n" + 
                "                this.options = options;\r\n" + 
                "                this.simulateEvent(elem, options);\r\n" + 
                "        };\r\n" + 
                "        jquery.extend(jquery.simulateDragDrop.prototype, {\r\n" + 
                "                simulateEvent: function(elem, options) {\r\n" + 
                "                        /*Simulating drag start*/\r\n" + 
                "                        var type = 'dragstart';\r\n" + 
                "                        var event = this.createEvent(type);\r\n" + 
                "                        this.dispatchEvent(elem, type, event);\r\n" + 
                "\r\n" + 
                "                        /*Simulating drop*/\r\n" + 
                "                        type = 'drop';\r\n" + 
                "                        var dropEvent = this.createEvent(type, {});\r\n" + 
                "                        dropEvent.dataTransfer = event.dataTransfer;\r\n" + 
                "                        this.dispatchEvent(jquery(options.dropTarget)[0], type, dropEvent);\r\n" + 
                "\r\n" + 
                "                        /*Simulating drag end*/\r\n" + 
                "                        type = 'dragend';\r\n" + 
                "                        var dragEndEvent = this.createEvent(type, {});\r\n" + 
                "                        dragEndEvent.dataTransfer = event.dataTransfer;\r\n" + 
                "                        this.dispatchEvent(elem, type, dragEndEvent);\r\n" + 
                "                },\r\n" + 
                "                createEvent: function(type) {\r\n" + 
                "                        var event = document.createEvent(\"CustomEvent\");\r\n" + 
                "                        event.initCustomEvent(type, true, true, null);\r\n" + 
                "                        event.dataTransfer = {\r\n" + 
                "                                data: {\r\n" + 
                "                                },\r\n" + 
                "                                setData: function(type, val){\r\n" + 
                "                                        this.data[type] = val;\r\n" + 
                "                                },\r\n" + 
                "                                getData: function(type){\r\n" + 
                "                                        return this.data[type];\r\n" + 
                "                                }\r\n" + 
                "                        };\r\n" + 
                "                        return event;\r\n" + 
                "                },\r\n" + 
                "                dispatchEvent: function(elem, type, event) {\r\n" + 
                "                        if(elem.dispatchEvent) {\r\n" + 
                "                                elem.dispatchEvent(event);\r\n" + 
                "                        }else if( elem.fireEvent ) {\r\n" + 
                "                                elem.fireEvent(\"on\"+type, event);\r\n" + 
                "                        }\r\n" + 
                "                }\r\n" + 
                "        });\r\n" + 
                "})(jQuery);";
        ((JavascriptExecutor) driver).executeScript(jqueryScript);
        String dragAndDropScript = "jQuery('" + dragSourceJQuerySelector + "').simulateDragDrop({ dropTarget: '" + dropTargetJQuerySelector + "'});";
        ((JavascriptExecutor) driver).executeScript(dragAndDropScript);
    }

只需在参数中传递css或xpath选择器即可。 希望对您有帮助。 如果那对您没有帮助,我也可以为您提供其他解决方案。