Académique Documents
Professionnel Documents
Culture Documents
(https://www.guru99.com/)
Actions.dragAndDrop(Sourcelocator, Destinationlocator)
https://www.guru99.com/drag-drop-selenium.html 1/13
12/13/2018 Drag and Drop action in Selenium: dragAndDrop, dragAndDropBy
(/images/1/102717_0423_DragandDrop1.png)
Let's practically show you the drag and drop of an element using the selenium webdriver
with following 3 scenarios
https://www.guru99.com/drag-drop-selenium.html 2/13
12/13/2018 Drag and Drop action in Selenium: dragAndDrop, dragAndDropBy
(/images/1/102717_0423_DragandDrop2.png)
https://www.guru99.com/drag-drop-selenium.html 3/13
12/13/2018 Drag and Drop action in Selenium: dragAndDrop, dragAndDropBy
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;
import org.testng.annotations.Test;
WebDriver driver;
@Test
public void DragnDrop()
{
System.setProperty("webdriver.chrome.driver"," E://Selenium//Selenium_Jars//chrome
driver.exe ");
driver= new ChromeDriver();
driver.get("http://demo.guru99.com/test/drag_drop.html");
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."
WebElement From=driver.findElement(By.xpath("//*[@id='credit2']/a"));
Second, we capture the 2nd element on which we need to drop the 1st element in variable
"To".
WebElement To=driver.findElement(By.xpath("//*[@id='bank']/li"));
https://www.guru99.com/drag-drop-selenium.html 4/13
Third,
12/13/2018 we create object of Actions class
Drag asaction
and Drop weinuse methods
Selenium: of Actions
dragAndDrop, class.
dragAndDropBy
For drag and drop element we use dragAndDrop method of Actions class and passes the
parameters as the first element(Sourcelocator) "From" and the second
element(Destinationlocator) "To". Below line will drag the 1st element and drop it on the 2nd
element.
act.dragAndDrop(From, To).build().perform();
Now you can execute the above script one by one from eclipse as shown in below
screenshot.
(/images/1/102717_0423_DragandDrop3.png)
https://www.guru99.com/drag-drop-selenium.html 5/13
12/13/2018 Drag and Drop action in Selenium: dragAndDrop, dragAndDropBy
(/images/1/102717_0423_DragandDrop4.gif)
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.
https://www.guru99.com/drag-drop-selenium.html 6/13
12/13/2018 Drag and Drop action in Selenium: dragAndDrop, dragAndDropBy
(/images/1/102717_0423_DragandDrop5.png)
https://www.guru99.com/drag-drop-selenium.html 7/13
12/13/2018 Drag and Drop action in Selenium: dragAndDrop, dragAndDropBy
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;
import org.testng.annotations.Test;
WebDriver driver;
@Test
public void DragnDrop()
{
System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromed
river.exe");
driver= new ChromeDriver();
driver.get("http://demo.guru99.com/test/drag_drop.html");
NOTE: The pixels values change with screen resolution and browser size. This method is
hence not reliable and not widely used.
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.
https://www.guru99.com/drag-drop-selenium.html 8/13
12/13/2018 Drag and Drop action in Selenium: dragAndDrop, dragAndDropBy
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;
import org.testng.annotations.Test;
WebDriver driver;
@Test
public void DragnDrop()
{
System.setProperty("webdriver.chrome.driver"," E://Selenium//Selenium_Jars//chrome
driver.exe");
driver= new ChromeDriver();
driver.get("http://demo.guru99.com/test/drag_drop.html");
{
System.out.println("Perfect Displayed !!!");
}
else
{
System.out.println("Perfect not Displayed !!!");
}
}
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.
0:00 / 0:01
Summary
In the above tutorials, we illustrate the drag and drop functionality of the web application
through Action methods in Webdriver:
dragAndDrop(Sourcelocator, Destinationlocator)
https://www.guru99.com/drag-drop-selenium.html 10/13
dragAndDropBy(Sourcelocator,
12/13/2018 Drag x-axis pixelinof
and Drop action Destinationlocator,
Selenium: y-axis
dragAndDrop, dragAndDropBy 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.
Next (/selenium-csharp-tutorial.html)
Selenium Tutorials
https://www.guru99.com/drag-drop-selenium.html 11/13
42) SSL
12/13/2018 Certificate Error Handling (/ssl-certificate-error-handling-selenium.html)
Drag and Drop action in Selenium: dragAndDrop, dragAndDropBy
(https://www.facebook.com/guru99com/)
(https://twitter.com/guru99com)
(https://www.youtube.com/channel/UC19i1XD6k88KqHlET8atqFQ)
(https://forms.aweber.com/form/46/724807646.htm)
About
https://www.guru99.com/drag-drop-selenium.html 12/13
12/13/2018 Drag and Drop action in Selenium: dragAndDrop, dragAndDropBy
About US (/about-us.html)
Advertise with Us (/advertise-us.html)
Write For Us (/become-an-instructor.html)
Contact US (/contact-us.html)
Career Sugges on
SAP Career Suggestion Tool (/best-sap-module.html)
Software Testing as a Career (/software-testing-career-
complete-guide.html)
Certificates (/certificate-it-professional.html)
Interes ng
Books to Read! (/books.html)
Suggest a Tutorial
Blog (/blog/)
Quiz (/tests.html)
Review (/best-ergonomic-mouse.html)
Execute online
Execute Java Online (/try-java-editor.html)
Execute Javascript (/execute-javascript-online.html)
Execute HTML (/execute-html-online.html)
Execute Python (/execute-python-online.html)
https://www.guru99.com/drag-drop-selenium.html 13/13