Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform drag and drop using selenium-webdriver when target and destination element are in different frames?

I have to drag an image and drop it into a CQ5 component. The image and component are in different frames.

Here is the code which did not work as webelement destinationcould not be found when the target's frame was active.

new Actions(driver).dragAndDrop(target, destination).perform();

I have also tried to switch frame in between action as:

    Actions builder = new Actions(driver);
    Actions action = builder.clickAndHold(target);
    driver.switchTo().frame("newFrame"); //switching frames
    builder.moveToElement(destination);
    builder.release(destination);
    builder.build();
    action.perform();

This is did not work either. Then, I tried moving the image by offset

new Actions(driver).dragAndDropBy(target,  x, y).perform(); // x and y 

This moved the image but component did not capture it, probably becuase action was too fast. Is there any way such drag drop can be done?

Thanks in advance.

like image 696
9ikhan Avatar asked Aug 10 '12 07:08

9ikhan


People also ask

How do I drag and drop in different frames?

frame(frameOne); // identify element in first frame WebElement elementOne = driver. findElement(By.id("dragFrame-0")); // Use Actions class for tap and hold Actions actions = new Actions(driver); Actions action = actions. clickAndHold(elementOne); actions. build(); action.

How do you simulate an action of drag and drop action with a Selenium driver?

Click And Hold Action: dragAndDrop() method first performs click-and-hold at the location of the source element. Move Mouse Action: Then source element gets moved to the location of the target element. Button Release Action: Finally, it releases the mouse.


1 Answers

I had the same prb as you. I can't darg and drop two elements from one frame to anther. the ansers uppers are correct but from selenium 3 this solution doesn't works any more. The workarrown is to move source element (after clickAndHol) to the positon 0,0 and then to move it under the second frame. for example 150,150.

 Actions builder = new Actions(driver);
 // switsh to the source frame
 driver.switchTo().frame("sourceFrame");
 // take the element with mouse 
 builder.clickAndHold(sourceElt).build().perfom();
 // move mouse to the top of the source frame
 builder.moveToElement(sourceElt, 0, 0 ).build().perfom();
 // move the mouse under the target frame (specific to your case)
 builder.moveToElement(sourceElt, 150,200).build().perfom();
 // switsh to the target frame
 driver.switchTo().frame("targetFrame");
 builder.moveToElement(targetElt).build().perform();
 builder.release(target).build().perform();      

hope I helps you too.

like image 119
Ben Aissa Moez Avatar answered Sep 21 '22 17:09

Ben Aissa Moez