Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

java - Why drag and drop is not working in Selenium Webdriver?

I am trying to drag an element into another element using Selenium WebDriver but it's not working. I tried all the solutions which I can find on internet but none of the solutions seems to be working for me.

WebElement sourceelement = driver.findElement(By.cssSelector("XXX"));
WebElement destelement = driver.findElement(By.cssSelector("YYY"));

Code1:-

Actions builder = new Actions( _controls.getDriver());
builder.dragAndDrop(sourceelement, destelement);

Code2:-

Actions builder = new Actions(_controls.getDriver());
Action dragAndDrop =
builder.clickAndHold(sourceelement).moveToElement(destelement).release(destelement).build();
Thread.sleep(2000);
dragAndDrop.perform()

Code3:-

Point coordinates1 = sourceelement.getLocation();
Point coordinates2 = destelement.getLocation();  
Robot robot = new Robot();           
robot.mouseMove(coordinates1.getX(), coordinates1.getY());
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseMove(coordinates2.getX(), coordinates2.getY());
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Thread.sleep(2000);

Code4:-

final String java_script =
"var src=arguments[0],tgt=arguments[1];var dataTransfer={dropEffe" +
                "ct:'',effectAllowed:'all',files:[],items:{},types:[],setData:fun" +
                "ction(format,data){this.items[format]=data;this.types.append(for" +
                "mat);},getData:function(format){return this.items[format];},clea" +
                "rData:function(format){}};var emit=function(event,target){var ev" +
                "t=document.createEvent('Event');evt.initEvent(event,true,false);" +
                "evt.dataTransfer=dataTransfer;target.dispatchEvent(evt);};emit('" +
                "dragstart',src);emit('dragenter',tgt);emit('dragover',tgt);emit(" +
                "'drop',tgt);emit('dragend',src);";

        ((JavascriptExecutor)_controls.getDriver()).executeScript(java_script, sourceelement, destelement);
        Thread.sleep(2000);

None of the above code is working for me. All the above runs without any error but drag and drop is not happening in the application. Anyone having any other solution ? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Can you try Java Script Executor for this

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

Refrence: https://www.linkedin.com/pulse/javascriptexecutor-selenium-gaurav-gupta/

It works for me on web application which are angular based.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...