易百教程

35、在 WebDriver 中如何进行拖放操作?

执行拖放操作的代码片段:

//WebElement on which drag and drop operation needs to be performed  
WebElementfromWebElement = driver.findElement(By Locator of fromWebElement);  

//WebElement to which the above object is dropped  
WebElementtoWebElement = driver.findElement(By Locator of toWebElement);  

//Creating object of Actions class to build composite actions  
Actions builder = newActions(driver);  

//Building a drag and drop action  
Action dragAndDrop = builder.clickAndHold(fromWebElement)  
             .moveToElement(toWebElement)  
             .release(toWebElement)  
         .build();  

//Performing the drag and drop action  
dragAndDrop.perform();