Suppose we have a requirement to drag a webelement from one location and drop in another using a selenium script. In selenium webdriver, we can perform drag and drop or mouse operations by using Actions class.
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Action; import org.openqa.selenium.interactions.Actions; public class Training { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("http: //jqueryui. com/droppable/"); driver.manage().window().maximize(); driver.switchTo().frame(0); driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS); WebElement dragElement = driver.findElement(By.id("draggable")); WebElement dropElement = driver.findElement(By.id("droppable")); Actions builder = new Actions(driver); Action dragAndDrop = builder.clickAndHold(dragElement) .moveToElement(dropElement).release(dropElement).build(); dragAndDrop.perform(); } }