我试图使用Selenium WebDriver将元素拖到另一个元素中,但是它不起作用.我尝试了所有的解决方案,我可以在互联网上找到,但没有一个解决方案似乎在为我工作.
WebElement sourceelement = driver.findElement(By.cssSelector("XXX")); WebElement destelement = driver.findElement(By.cssSelector("YYY"));
代码1: –
Actions builder = new Actions( _controls.getDriver()); builder.dragAndDrop(sourceelement,destelement);
代码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);
码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);