asked 2 years ago
Drag and drop:
To perform drag and drop actions on a webpage, Selenium WebDriver provides a class called Actions. This class lets you simulate advanced user interactions like mouse movements, key presses, and drag-and-drop operations.
How Drag and Drop Works:
You can use the dragAndDrop() method of the Actions class by passing two elements:
Source element: The element you want to drag.
Target element: The element where you want to drop it.
Navigate to https://www.qafeast.com/demo,click the drag and drop tab.
Press F12 in the keyboard and inspect drag and drop.
Syntax
Example:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class DragAndDropExample {
    public static void main(String[] args) {
        // Set up the WebDriver
        WebDriver driver = new ChromeDriver();
        // Open QA Feast demo page
        driver.get("https://www.qafeast.com/demo/drag-and-drop.html");
        // Maximize the browser window
        driver.manage().window().maximize();
        // Locate the source and target elements
        WebElement sourceElement = driver.findElement(By.xpath("//p[text()='Python']"));
        WebElement targetElement = driver.findElement(By.xpath("//p[text()='Python.exe']"));
        // Perform drag and drop
        (new Actions(driver)).dragAndDrop(sourceElement, targetElement).perform();
        // Close the browser
        driver.quit();
    }
}