Syntax for drag and drop.
The Actions class has two methods that support Drag and Drop. Let’s study them- In dragAndDrop method, we pass the two parameters –
First parameter “Sourcelocator” is the element which we need to drag Second parameter “Destinationlocator” is the element on which we need to drop the first element
dragAndDropBy method we pass the 3 parameters –
First parameter “Sourcelocator” is the element which we need to drag The second parameter is x-axis pixel value of the 2nd element on which we need to drop the first element. The third parameter is y-axis pixel value of the 2nd element on which we need to drop the first element.
Let’s practically show you the drag and drop of an element using the selenium webdriver with following 3 scenarios
Scenario 1: BANK element is dragged and dropped on the specific element by DragAndDrop method. Scenario 2: BANK element is dragged and dropped on the specific element by DragAndDrop method. Scenario 3: Few elements are dragged and dropped and then verify the message is displayed or not.
Scenario 1: BANK element is dragged and dropped on the specific cell by DragAndDrop method.
In the following code, we launch the given URL in Firefox browser and then drag the BANK element and drop on the DEBIT SIDE block through dragAndDrop method.
Code Explanation: In the above code we launch the given URL in Firefox browser and then drag the BANK element and drop on the DEBIT SIDE block through dragAndDrop method. Explained briefly below: First, we capture the 1st element which we need to drag in variable “From.” Second, we capture the 2nd element on which we need to drop the 1st element in variable “To”. Third, we create object of Actions class as we use methods of Actions class.
act.dragAndDrop(From, To).build().perform(); Execution of the script. Now you can execute the above script one by one from eclipse as shown in below screenshot.
Here is the output when you run the script
Scenario 2: BANK element is dragged and dropped on the specific cell by DragAndDrop method.
In this scenario, we launch the given URL in the browser and then drag the BANK element and drop on the DEBIT SIDE block through dragAndDropBy method. To dragAndDropBy, we need to find the pixel of the element. How to find Pixel? Open the URL in Chrome or FireFox and click on the Blue color arrow. Next click on any element for which you want to know the pixel. You will find the pixel above the element as shown in below screenshot.
Scenario 3: Few elements are dragged and dropped and then verify the message is displayed or not.
In the following code, we launch the given URL in the browser and then drag the elements like BANK, SALES, 500 and drop on the respective block. Once done we verify the output message. Output analysis In Output, you can see the element is dragged and dropped on the defined element. You can check the GIF of the output. Your browser does not support the video tag.
Summary
In the above tutorials, we illustrate the drag and drop functionality of the web application through Action methods in Webdriver: dragAndDrop(Sourcelocator, Destinationlocator) dragAndDropBy(Sourcelocator, x-axis pixel of Destinationlocator, y-axis pixel of Destinationlocator) To drag and drop the element first we used DragAndDrop method from the Actions class in which we pass the 2 parameters, 1st parameter is the element which we need to drag, and 2nd parameter is the element on which we need to drop the 1st element. Second, we used the dragAndDropBy method from the Actions class in which we pass the 3 parameters, the 1st parameter is the element which we need to drag, 2nd parameter is the x-axis pixel value of the 2nd element, 3rd parameter is the y-axis pixel value of the 2nd element.