Monday, 12 March 2018

How to Drag and Drop Element in Selenium

Sometime in testing, we need to do some operations with mouse and keyboard. 
Selenium web driver directly can not handle mouse and keyboard event like double click, right click, pressing key combination in keyboard.

We can handle such type of event with Actions class in selenium webdriver. We have to create object of that class and perform action which we want.

In this article, we will understand how to do drag and drop any element using selenium.

We have to use Actions class for drag and drop any element form one place to another place.There are two methods for drag and drop element in Actions class.

Method 1:

action.dragAndDrop(source, target);

Method 2:

action.dragAndDropBy(source, xOffset, yOffset);

Here, action is object of Actions class.

In first method, you have to pass two web elements. One is source element and second is target element at where you want to drop your source element.

In Second method, You have to pass only source web element. For target element you have to pass value of X coordinate and Y coordinate of target element. Value of  X coordinate and Y coordinate are Integer value.

Please add comment if you have any question.



Popular