Java – why doesn’t drag and drop work in selenium webdriver?
•
Java
I tried to drag an element into another element using selenium webdriver, but it didn't work I tried all the solutions I could find on the Internet, but none seemed to work for me
WebElement sourceelement = driver.findElement(By.cssSelector("XXX")); WebElement destelement = driver.findElement(By.cssSelector("YYY"));
Code 1: –
Actions builder = new Actions( _controls.getDriver()); builder.dragAndDrop(sourceelement,destelement);
Code 2: –
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);
Code 4: –
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 codes work for me All the above runs have no errors, but drag and drop does not occur in the application Does anyone have any other solutions? thank you.
Solution
In your code 1:
In your code 2: I don't think you need to call release()
Please search for similar questions before publishing
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码