Is there any way to drag and drop an element from one frame to another?
Hope you doing well,
I'm trying to dragAndDrop an element from FrameOne to FrameTwo but not able to do so.Please help me to understand the concept and what I'm doing wrong here and need to achieve the task by using Actions class only.
Here're the URLs :
- https://www.w3schools.com/html/html5_draganddrop.asp
Here the element is in a div block I'm able to get all the locaters and do all other actions using Actions class but not able to drag and drop the element.
2.https://codepen.io/rjsmer/full/vvewWp
Here I'm trying to move the element from Frame one to Frame two but I'm not able to do so.
I've tried drangAndDrop(),ClickAndHold() methods,Searched so many solutions, watch videos on the same with no success.
package DragAndDropPracticeFrame;
import io.github.bonigarcia.wdm.WebDriverManager;
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.Action;
import org.openqa.selenium.interactions.Actions;
import static io.github.bonigarcia.wdm.DriverManagerType.CHROME;
public class DragDropFrame {
public static void main(String args) throws InterruptedException {
WebDriverManager.getInstance(CHROME).setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://codepen.io/rjsmer/full/vvewWp");
driver.switchTo().frame("result");
System.out.println("Inside First Frame.");
WebElement frameOne =
driver.findElement(By.cssSelector("iframe.dragFrame.dragDrop"));
driver.switchTo().frame(frameOne);
System.out.println("Inside Frame 3");
WebElement elementOne = driver.findElement(By.id("dragFrame-0"));
System.out.println("First element found: " + elementOne.getText());
Actions builder = new Actions(driver);
driver.switchTo().defaultContent();
System.out.println("Inside main page");
driver.switchTo().frame("result");
//System.out.println("Switched to Frame First");
WebElement frameThree =
driver.findElement(By.xpath("//iframe[@class='dropFrame dragDrop']"));
Action action =
builder.clickAndHold(elementOne)
.moveToElement(frameThree)
.release(frameThree).build();
//driver.switchTo().frame(frameTwo);
//System.out.println("Switched to frame 3");
action.perform();
//driver.switchTo().defaultContent();
//builder.perform();
}
}
Another try :
WebDriverManager.getInstance(CHROME).setup();
WebDriver driver = new ChromeDriver();
driver.get("https://codepen.io/rjsmer/full/vvewWp");
driver.switchTo().frame(0);
WebElement frameOne = driver.findElement(By.xpath("//iframe[@class='dragFrame dragDrop']"));
WebElement frameTwo = driver.findElement(By.xpath("//iframe[@class='dropFrame dragDrop']"));
driver.switchTo().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.perform();
// switch to the second frame
driver.switchTo().frame(frameTwo);
// move element to another frame
WebElement elementTwo = driver.findElement(By.xpath("//body[@class='frameBody dropFrameBody']"));
Actions actions2 = new Actions(driver);
Actions action2 = actions2.moveToElement(elementTwo);
actions2.release(elementOne);
actions2.build();
action2.perform();
Expected: The element should move to Frame 3
Actual: Nothing happened.
java selenium selenium-webdriver
add a comment |
Hope you doing well,
I'm trying to dragAndDrop an element from FrameOne to FrameTwo but not able to do so.Please help me to understand the concept and what I'm doing wrong here and need to achieve the task by using Actions class only.
Here're the URLs :
- https://www.w3schools.com/html/html5_draganddrop.asp
Here the element is in a div block I'm able to get all the locaters and do all other actions using Actions class but not able to drag and drop the element.
2.https://codepen.io/rjsmer/full/vvewWp
Here I'm trying to move the element from Frame one to Frame two but I'm not able to do so.
I've tried drangAndDrop(),ClickAndHold() methods,Searched so many solutions, watch videos on the same with no success.
package DragAndDropPracticeFrame;
import io.github.bonigarcia.wdm.WebDriverManager;
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.Action;
import org.openqa.selenium.interactions.Actions;
import static io.github.bonigarcia.wdm.DriverManagerType.CHROME;
public class DragDropFrame {
public static void main(String args) throws InterruptedException {
WebDriverManager.getInstance(CHROME).setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://codepen.io/rjsmer/full/vvewWp");
driver.switchTo().frame("result");
System.out.println("Inside First Frame.");
WebElement frameOne =
driver.findElement(By.cssSelector("iframe.dragFrame.dragDrop"));
driver.switchTo().frame(frameOne);
System.out.println("Inside Frame 3");
WebElement elementOne = driver.findElement(By.id("dragFrame-0"));
System.out.println("First element found: " + elementOne.getText());
Actions builder = new Actions(driver);
driver.switchTo().defaultContent();
System.out.println("Inside main page");
driver.switchTo().frame("result");
//System.out.println("Switched to Frame First");
WebElement frameThree =
driver.findElement(By.xpath("//iframe[@class='dropFrame dragDrop']"));
Action action =
builder.clickAndHold(elementOne)
.moveToElement(frameThree)
.release(frameThree).build();
//driver.switchTo().frame(frameTwo);
//System.out.println("Switched to frame 3");
action.perform();
//driver.switchTo().defaultContent();
//builder.perform();
}
}
Another try :
WebDriverManager.getInstance(CHROME).setup();
WebDriver driver = new ChromeDriver();
driver.get("https://codepen.io/rjsmer/full/vvewWp");
driver.switchTo().frame(0);
WebElement frameOne = driver.findElement(By.xpath("//iframe[@class='dragFrame dragDrop']"));
WebElement frameTwo = driver.findElement(By.xpath("//iframe[@class='dropFrame dragDrop']"));
driver.switchTo().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.perform();
// switch to the second frame
driver.switchTo().frame(frameTwo);
// move element to another frame
WebElement elementTwo = driver.findElement(By.xpath("//body[@class='frameBody dropFrameBody']"));
Actions actions2 = new Actions(driver);
Actions action2 = actions2.moveToElement(elementTwo);
actions2.release(elementOne);
actions2.build();
action2.perform();
Expected: The element should move to Frame 3
Actual: Nothing happened.
java selenium selenium-webdriver
have you tried driver.dragAndDrop(source, destination) ?
– Dheemanth Bhandarkar
Dec 31 '18 at 8:38
Yes, I've tried that as well
– Sameer Malhotra
Dec 31 '18 at 9:19
add a comment |
Hope you doing well,
I'm trying to dragAndDrop an element from FrameOne to FrameTwo but not able to do so.Please help me to understand the concept and what I'm doing wrong here and need to achieve the task by using Actions class only.
Here're the URLs :
- https://www.w3schools.com/html/html5_draganddrop.asp
Here the element is in a div block I'm able to get all the locaters and do all other actions using Actions class but not able to drag and drop the element.
2.https://codepen.io/rjsmer/full/vvewWp
Here I'm trying to move the element from Frame one to Frame two but I'm not able to do so.
I've tried drangAndDrop(),ClickAndHold() methods,Searched so many solutions, watch videos on the same with no success.
package DragAndDropPracticeFrame;
import io.github.bonigarcia.wdm.WebDriverManager;
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.Action;
import org.openqa.selenium.interactions.Actions;
import static io.github.bonigarcia.wdm.DriverManagerType.CHROME;
public class DragDropFrame {
public static void main(String args) throws InterruptedException {
WebDriverManager.getInstance(CHROME).setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://codepen.io/rjsmer/full/vvewWp");
driver.switchTo().frame("result");
System.out.println("Inside First Frame.");
WebElement frameOne =
driver.findElement(By.cssSelector("iframe.dragFrame.dragDrop"));
driver.switchTo().frame(frameOne);
System.out.println("Inside Frame 3");
WebElement elementOne = driver.findElement(By.id("dragFrame-0"));
System.out.println("First element found: " + elementOne.getText());
Actions builder = new Actions(driver);
driver.switchTo().defaultContent();
System.out.println("Inside main page");
driver.switchTo().frame("result");
//System.out.println("Switched to Frame First");
WebElement frameThree =
driver.findElement(By.xpath("//iframe[@class='dropFrame dragDrop']"));
Action action =
builder.clickAndHold(elementOne)
.moveToElement(frameThree)
.release(frameThree).build();
//driver.switchTo().frame(frameTwo);
//System.out.println("Switched to frame 3");
action.perform();
//driver.switchTo().defaultContent();
//builder.perform();
}
}
Another try :
WebDriverManager.getInstance(CHROME).setup();
WebDriver driver = new ChromeDriver();
driver.get("https://codepen.io/rjsmer/full/vvewWp");
driver.switchTo().frame(0);
WebElement frameOne = driver.findElement(By.xpath("//iframe[@class='dragFrame dragDrop']"));
WebElement frameTwo = driver.findElement(By.xpath("//iframe[@class='dropFrame dragDrop']"));
driver.switchTo().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.perform();
// switch to the second frame
driver.switchTo().frame(frameTwo);
// move element to another frame
WebElement elementTwo = driver.findElement(By.xpath("//body[@class='frameBody dropFrameBody']"));
Actions actions2 = new Actions(driver);
Actions action2 = actions2.moveToElement(elementTwo);
actions2.release(elementOne);
actions2.build();
action2.perform();
Expected: The element should move to Frame 3
Actual: Nothing happened.
java selenium selenium-webdriver
Hope you doing well,
I'm trying to dragAndDrop an element from FrameOne to FrameTwo but not able to do so.Please help me to understand the concept and what I'm doing wrong here and need to achieve the task by using Actions class only.
Here're the URLs :
- https://www.w3schools.com/html/html5_draganddrop.asp
Here the element is in a div block I'm able to get all the locaters and do all other actions using Actions class but not able to drag and drop the element.
2.https://codepen.io/rjsmer/full/vvewWp
Here I'm trying to move the element from Frame one to Frame two but I'm not able to do so.
I've tried drangAndDrop(),ClickAndHold() methods,Searched so many solutions, watch videos on the same with no success.
package DragAndDropPracticeFrame;
import io.github.bonigarcia.wdm.WebDriverManager;
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.Action;
import org.openqa.selenium.interactions.Actions;
import static io.github.bonigarcia.wdm.DriverManagerType.CHROME;
public class DragDropFrame {
public static void main(String args) throws InterruptedException {
WebDriverManager.getInstance(CHROME).setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://codepen.io/rjsmer/full/vvewWp");
driver.switchTo().frame("result");
System.out.println("Inside First Frame.");
WebElement frameOne =
driver.findElement(By.cssSelector("iframe.dragFrame.dragDrop"));
driver.switchTo().frame(frameOne);
System.out.println("Inside Frame 3");
WebElement elementOne = driver.findElement(By.id("dragFrame-0"));
System.out.println("First element found: " + elementOne.getText());
Actions builder = new Actions(driver);
driver.switchTo().defaultContent();
System.out.println("Inside main page");
driver.switchTo().frame("result");
//System.out.println("Switched to Frame First");
WebElement frameThree =
driver.findElement(By.xpath("//iframe[@class='dropFrame dragDrop']"));
Action action =
builder.clickAndHold(elementOne)
.moveToElement(frameThree)
.release(frameThree).build();
//driver.switchTo().frame(frameTwo);
//System.out.println("Switched to frame 3");
action.perform();
//driver.switchTo().defaultContent();
//builder.perform();
}
}
Another try :
WebDriverManager.getInstance(CHROME).setup();
WebDriver driver = new ChromeDriver();
driver.get("https://codepen.io/rjsmer/full/vvewWp");
driver.switchTo().frame(0);
WebElement frameOne = driver.findElement(By.xpath("//iframe[@class='dragFrame dragDrop']"));
WebElement frameTwo = driver.findElement(By.xpath("//iframe[@class='dropFrame dragDrop']"));
driver.switchTo().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.perform();
// switch to the second frame
driver.switchTo().frame(frameTwo);
// move element to another frame
WebElement elementTwo = driver.findElement(By.xpath("//body[@class='frameBody dropFrameBody']"));
Actions actions2 = new Actions(driver);
Actions action2 = actions2.moveToElement(elementTwo);
actions2.release(elementOne);
actions2.build();
action2.perform();
Expected: The element should move to Frame 3
Actual: Nothing happened.
java selenium selenium-webdriver
java selenium selenium-webdriver
edited Dec 31 '18 at 12:59
Sameer Malhotra
asked Dec 30 '18 at 20:10
Sameer MalhotraSameer Malhotra
256
256
have you tried driver.dragAndDrop(source, destination) ?
– Dheemanth Bhandarkar
Dec 31 '18 at 8:38
Yes, I've tried that as well
– Sameer Malhotra
Dec 31 '18 at 9:19
add a comment |
have you tried driver.dragAndDrop(source, destination) ?
– Dheemanth Bhandarkar
Dec 31 '18 at 8:38
Yes, I've tried that as well
– Sameer Malhotra
Dec 31 '18 at 9:19
have you tried driver.dragAndDrop(source, destination) ?
– Dheemanth Bhandarkar
Dec 31 '18 at 8:38
have you tried driver.dragAndDrop(source, destination) ?
– Dheemanth Bhandarkar
Dec 31 '18 at 8:38
Yes, I've tried that as well
– Sameer Malhotra
Dec 31 '18 at 9:19
Yes, I've tried that as well
– Sameer Malhotra
Dec 31 '18 at 9:19
add a comment |
3 Answers
3
active
oldest
votes
After many trails using Actions class, I understood that Actions class cannot be used drag and drop elements across frames. So, I switched to using Robot Class and it worked!
//Setting up chrome driver
WebDriverManager.getInstance(CHROME).setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
//Redirecting to the website
driver.get("https://codepen.io/rjsmer/full/vvewWp");
Robot robot = new Robot();
robot.mouseMove(120, 300);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(2000);
robot.mouseMove(500, 320);
Thread.sleep(2000);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
The sleeps here are important as the commands are being implemented in a quick fashion and it helps the Robot class in pacing its commands accurately.
Depending on the dragFrame you want to drag, you can use the respective coordinates.
Thank you Uday for your support, I've been trying to do it using action class since 29th December. How did you managed to get the coordinates like (120,300)?
– Sameer Malhotra
Jan 3 at 3:09
I have used this extension to get the coordinates: chrome.google.com/webstore/detail/coordinates/…
– Satya
Jan 3 at 3:17
add a comment |
What if first you identify first frame and use Actions
class. There you can do tap and hold. Then switch to another frame, and use move to element.
// move to first frame
driver.switchTo().frame("frameOne");
// identify element in first frame
WebElement elementOne = driver.findElement(By.xpath(XPATH));
// Use Actions class for tap and hold
Actions actions = new Actions(driver);
Actions action = actions.clickAndHold(elementOne);
actions.build();
action.perform();
// switch to the second frame
driver.switchTo().frame("frameTwo");
// move element to another frame
Actions actions2 = new Actions(driver);
Actions action2 = actions2.moveToElement(elementTwo);
actions2.release(elementTwo);
actions2.build();
action2.perform();
Now getting following exception : Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
– Sameer Malhotra
Dec 31 '18 at 12:57
where you are getting this exception?
– Sanjay Bhimani
Dec 31 '18 at 13:01
On this line driver.switchTo().frame(frameTwo);
– Sameer Malhotra
Dec 31 '18 at 13:08
After adding these three lines driver.switchTo().defaultContent(); driver.switchTo().frame(0); driver.switchTo().frame(frameTwo); I'm not getting any exception but it's not doing drag and drop
– Sameer Malhotra
Dec 31 '18 at 13:10
Can you check your elementOne and elementTwo locators are ok. Means there should be drag and drop working manually.
– Sanjay Bhimani
Dec 31 '18 at 13:12
|
show 1 more comment
I noticed that on running above code, if you move mouse, you can see the dragged element attached to mouse. Try using Robot library and perform this with Robot library mouse actions.
See https://www.guru99.com/using-robot-api-selenium.html
I can't test this on my end as Robot library has bug on mac and i am using mac, if you are using windows, you can try:
Point browserLoc = driver.manage().window().getPosition();
driver.switchTo().frame(0);
WebElement frame1 = driver.findElement(By.xpath("//iframe[1]"));
Point frameLoc = frame1.getLocation();
driver.switchTo().frame(frame1);
Thread.sleep(1000);
WebElement listElement1 = driver.findElement(By.id("dragFrame-2"));
Point elementLoc = listElement1.getLocation();
Dimension elementSize = listElement1.getSize();
int x = elementLoc.getX() + frameLoc.getX() + browserLoc.getX() + elementSize.getWidth()/2;
int y = elementLoc.getY() + frameLoc.getY() + browserLoc.getY() + elementSize.getHeight()/2;
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.mouseMove(x, y);
Actions action = new Actions(driver);
action.clickAndHold(listElement1);
Action a = action.build();
a.perform();
Thread.sleep(1000);
driver.switchTo().defaultContent();
driver.switchTo().frame(0);
WebElement frame2 = driver.findElement(By.xpath("//iframe[2]"));
Point frameLoc2 = frame2.getLocation();
driver.switchTo().frame(frame2);
WebElement frame2Body = driver.findElement(By.xpath("//body"));
Point frame2BodyLoc = frame2Body.getLocation();
Dimension frame2BodySize = frame2Body.getSize();
int x1 = frame2BodyLoc.getX() + frameLoc2.getX() + browserLoc.getX() + frame2BodySize.getWidth()/2;
int y1 = frame2BodyLoc.getY() + frameLoc2.getY() + browserLoc.getY() + frame2BodySize.getHeight()/2;
robot.setAutoDelay(50);
robot.mouseMove(x1, y1);
action.moveToElement(frame2Body);
action.release();
a = action.build();
a.perform();
I tried doing it as per your feedback, however it's still same the element is getting dragged but it's not dropping it.
– Sameer Malhotra
Jan 2 at 7:57
Yes, try action.click(frame2Body) after action.release()
– sameeksha sahib
Jan 2 at 10:38
I tried that but it didn't worked solution from Uday worked, Thank you very much for your support.
– Sameer Malhotra
Jan 3 at 3:12
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53981055%2fis-there-any-way-to-drag-and-drop-an-element-from-one-frame-to-another%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
After many trails using Actions class, I understood that Actions class cannot be used drag and drop elements across frames. So, I switched to using Robot Class and it worked!
//Setting up chrome driver
WebDriverManager.getInstance(CHROME).setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
//Redirecting to the website
driver.get("https://codepen.io/rjsmer/full/vvewWp");
Robot robot = new Robot();
robot.mouseMove(120, 300);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(2000);
robot.mouseMove(500, 320);
Thread.sleep(2000);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
The sleeps here are important as the commands are being implemented in a quick fashion and it helps the Robot class in pacing its commands accurately.
Depending on the dragFrame you want to drag, you can use the respective coordinates.
Thank you Uday for your support, I've been trying to do it using action class since 29th December. How did you managed to get the coordinates like (120,300)?
– Sameer Malhotra
Jan 3 at 3:09
I have used this extension to get the coordinates: chrome.google.com/webstore/detail/coordinates/…
– Satya
Jan 3 at 3:17
add a comment |
After many trails using Actions class, I understood that Actions class cannot be used drag and drop elements across frames. So, I switched to using Robot Class and it worked!
//Setting up chrome driver
WebDriverManager.getInstance(CHROME).setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
//Redirecting to the website
driver.get("https://codepen.io/rjsmer/full/vvewWp");
Robot robot = new Robot();
robot.mouseMove(120, 300);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(2000);
robot.mouseMove(500, 320);
Thread.sleep(2000);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
The sleeps here are important as the commands are being implemented in a quick fashion and it helps the Robot class in pacing its commands accurately.
Depending on the dragFrame you want to drag, you can use the respective coordinates.
Thank you Uday for your support, I've been trying to do it using action class since 29th December. How did you managed to get the coordinates like (120,300)?
– Sameer Malhotra
Jan 3 at 3:09
I have used this extension to get the coordinates: chrome.google.com/webstore/detail/coordinates/…
– Satya
Jan 3 at 3:17
add a comment |
After many trails using Actions class, I understood that Actions class cannot be used drag and drop elements across frames. So, I switched to using Robot Class and it worked!
//Setting up chrome driver
WebDriverManager.getInstance(CHROME).setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
//Redirecting to the website
driver.get("https://codepen.io/rjsmer/full/vvewWp");
Robot robot = new Robot();
robot.mouseMove(120, 300);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(2000);
robot.mouseMove(500, 320);
Thread.sleep(2000);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
The sleeps here are important as the commands are being implemented in a quick fashion and it helps the Robot class in pacing its commands accurately.
Depending on the dragFrame you want to drag, you can use the respective coordinates.
After many trails using Actions class, I understood that Actions class cannot be used drag and drop elements across frames. So, I switched to using Robot Class and it worked!
//Setting up chrome driver
WebDriverManager.getInstance(CHROME).setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
//Redirecting to the website
driver.get("https://codepen.io/rjsmer/full/vvewWp");
Robot robot = new Robot();
robot.mouseMove(120, 300);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(2000);
robot.mouseMove(500, 320);
Thread.sleep(2000);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
The sleeps here are important as the commands are being implemented in a quick fashion and it helps the Robot class in pacing its commands accurately.
Depending on the dragFrame you want to drag, you can use the respective coordinates.
answered Jan 3 at 1:14


SatyaSatya
1047
1047
Thank you Uday for your support, I've been trying to do it using action class since 29th December. How did you managed to get the coordinates like (120,300)?
– Sameer Malhotra
Jan 3 at 3:09
I have used this extension to get the coordinates: chrome.google.com/webstore/detail/coordinates/…
– Satya
Jan 3 at 3:17
add a comment |
Thank you Uday for your support, I've been trying to do it using action class since 29th December. How did you managed to get the coordinates like (120,300)?
– Sameer Malhotra
Jan 3 at 3:09
I have used this extension to get the coordinates: chrome.google.com/webstore/detail/coordinates/…
– Satya
Jan 3 at 3:17
Thank you Uday for your support, I've been trying to do it using action class since 29th December. How did you managed to get the coordinates like (120,300)?
– Sameer Malhotra
Jan 3 at 3:09
Thank you Uday for your support, I've been trying to do it using action class since 29th December. How did you managed to get the coordinates like (120,300)?
– Sameer Malhotra
Jan 3 at 3:09
I have used this extension to get the coordinates: chrome.google.com/webstore/detail/coordinates/…
– Satya
Jan 3 at 3:17
I have used this extension to get the coordinates: chrome.google.com/webstore/detail/coordinates/…
– Satya
Jan 3 at 3:17
add a comment |
What if first you identify first frame and use Actions
class. There you can do tap and hold. Then switch to another frame, and use move to element.
// move to first frame
driver.switchTo().frame("frameOne");
// identify element in first frame
WebElement elementOne = driver.findElement(By.xpath(XPATH));
// Use Actions class for tap and hold
Actions actions = new Actions(driver);
Actions action = actions.clickAndHold(elementOne);
actions.build();
action.perform();
// switch to the second frame
driver.switchTo().frame("frameTwo");
// move element to another frame
Actions actions2 = new Actions(driver);
Actions action2 = actions2.moveToElement(elementTwo);
actions2.release(elementTwo);
actions2.build();
action2.perform();
Now getting following exception : Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
– Sameer Malhotra
Dec 31 '18 at 12:57
where you are getting this exception?
– Sanjay Bhimani
Dec 31 '18 at 13:01
On this line driver.switchTo().frame(frameTwo);
– Sameer Malhotra
Dec 31 '18 at 13:08
After adding these three lines driver.switchTo().defaultContent(); driver.switchTo().frame(0); driver.switchTo().frame(frameTwo); I'm not getting any exception but it's not doing drag and drop
– Sameer Malhotra
Dec 31 '18 at 13:10
Can you check your elementOne and elementTwo locators are ok. Means there should be drag and drop working manually.
– Sanjay Bhimani
Dec 31 '18 at 13:12
|
show 1 more comment
What if first you identify first frame and use Actions
class. There you can do tap and hold. Then switch to another frame, and use move to element.
// move to first frame
driver.switchTo().frame("frameOne");
// identify element in first frame
WebElement elementOne = driver.findElement(By.xpath(XPATH));
// Use Actions class for tap and hold
Actions actions = new Actions(driver);
Actions action = actions.clickAndHold(elementOne);
actions.build();
action.perform();
// switch to the second frame
driver.switchTo().frame("frameTwo");
// move element to another frame
Actions actions2 = new Actions(driver);
Actions action2 = actions2.moveToElement(elementTwo);
actions2.release(elementTwo);
actions2.build();
action2.perform();
Now getting following exception : Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
– Sameer Malhotra
Dec 31 '18 at 12:57
where you are getting this exception?
– Sanjay Bhimani
Dec 31 '18 at 13:01
On this line driver.switchTo().frame(frameTwo);
– Sameer Malhotra
Dec 31 '18 at 13:08
After adding these three lines driver.switchTo().defaultContent(); driver.switchTo().frame(0); driver.switchTo().frame(frameTwo); I'm not getting any exception but it's not doing drag and drop
– Sameer Malhotra
Dec 31 '18 at 13:10
Can you check your elementOne and elementTwo locators are ok. Means there should be drag and drop working manually.
– Sanjay Bhimani
Dec 31 '18 at 13:12
|
show 1 more comment
What if first you identify first frame and use Actions
class. There you can do tap and hold. Then switch to another frame, and use move to element.
// move to first frame
driver.switchTo().frame("frameOne");
// identify element in first frame
WebElement elementOne = driver.findElement(By.xpath(XPATH));
// Use Actions class for tap and hold
Actions actions = new Actions(driver);
Actions action = actions.clickAndHold(elementOne);
actions.build();
action.perform();
// switch to the second frame
driver.switchTo().frame("frameTwo");
// move element to another frame
Actions actions2 = new Actions(driver);
Actions action2 = actions2.moveToElement(elementTwo);
actions2.release(elementTwo);
actions2.build();
action2.perform();
What if first you identify first frame and use Actions
class. There you can do tap and hold. Then switch to another frame, and use move to element.
// move to first frame
driver.switchTo().frame("frameOne");
// identify element in first frame
WebElement elementOne = driver.findElement(By.xpath(XPATH));
// Use Actions class for tap and hold
Actions actions = new Actions(driver);
Actions action = actions.clickAndHold(elementOne);
actions.build();
action.perform();
// switch to the second frame
driver.switchTo().frame("frameTwo");
// move element to another frame
Actions actions2 = new Actions(driver);
Actions action2 = actions2.moveToElement(elementTwo);
actions2.release(elementTwo);
actions2.build();
action2.perform();
answered Dec 31 '18 at 12:41


Sanjay BhimaniSanjay Bhimani
1,2491126
1,2491126
Now getting following exception : Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
– Sameer Malhotra
Dec 31 '18 at 12:57
where you are getting this exception?
– Sanjay Bhimani
Dec 31 '18 at 13:01
On this line driver.switchTo().frame(frameTwo);
– Sameer Malhotra
Dec 31 '18 at 13:08
After adding these three lines driver.switchTo().defaultContent(); driver.switchTo().frame(0); driver.switchTo().frame(frameTwo); I'm not getting any exception but it's not doing drag and drop
– Sameer Malhotra
Dec 31 '18 at 13:10
Can you check your elementOne and elementTwo locators are ok. Means there should be drag and drop working manually.
– Sanjay Bhimani
Dec 31 '18 at 13:12
|
show 1 more comment
Now getting following exception : Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
– Sameer Malhotra
Dec 31 '18 at 12:57
where you are getting this exception?
– Sanjay Bhimani
Dec 31 '18 at 13:01
On this line driver.switchTo().frame(frameTwo);
– Sameer Malhotra
Dec 31 '18 at 13:08
After adding these three lines driver.switchTo().defaultContent(); driver.switchTo().frame(0); driver.switchTo().frame(frameTwo); I'm not getting any exception but it's not doing drag and drop
– Sameer Malhotra
Dec 31 '18 at 13:10
Can you check your elementOne and elementTwo locators are ok. Means there should be drag and drop working manually.
– Sanjay Bhimani
Dec 31 '18 at 13:12
Now getting following exception : Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
– Sameer Malhotra
Dec 31 '18 at 12:57
Now getting following exception : Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
– Sameer Malhotra
Dec 31 '18 at 12:57
where you are getting this exception?
– Sanjay Bhimani
Dec 31 '18 at 13:01
where you are getting this exception?
– Sanjay Bhimani
Dec 31 '18 at 13:01
On this line driver.switchTo().frame(frameTwo);
– Sameer Malhotra
Dec 31 '18 at 13:08
On this line driver.switchTo().frame(frameTwo);
– Sameer Malhotra
Dec 31 '18 at 13:08
After adding these three lines driver.switchTo().defaultContent(); driver.switchTo().frame(0); driver.switchTo().frame(frameTwo); I'm not getting any exception but it's not doing drag and drop
– Sameer Malhotra
Dec 31 '18 at 13:10
After adding these three lines driver.switchTo().defaultContent(); driver.switchTo().frame(0); driver.switchTo().frame(frameTwo); I'm not getting any exception but it's not doing drag and drop
– Sameer Malhotra
Dec 31 '18 at 13:10
Can you check your elementOne and elementTwo locators are ok. Means there should be drag and drop working manually.
– Sanjay Bhimani
Dec 31 '18 at 13:12
Can you check your elementOne and elementTwo locators are ok. Means there should be drag and drop working manually.
– Sanjay Bhimani
Dec 31 '18 at 13:12
|
show 1 more comment
I noticed that on running above code, if you move mouse, you can see the dragged element attached to mouse. Try using Robot library and perform this with Robot library mouse actions.
See https://www.guru99.com/using-robot-api-selenium.html
I can't test this on my end as Robot library has bug on mac and i am using mac, if you are using windows, you can try:
Point browserLoc = driver.manage().window().getPosition();
driver.switchTo().frame(0);
WebElement frame1 = driver.findElement(By.xpath("//iframe[1]"));
Point frameLoc = frame1.getLocation();
driver.switchTo().frame(frame1);
Thread.sleep(1000);
WebElement listElement1 = driver.findElement(By.id("dragFrame-2"));
Point elementLoc = listElement1.getLocation();
Dimension elementSize = listElement1.getSize();
int x = elementLoc.getX() + frameLoc.getX() + browserLoc.getX() + elementSize.getWidth()/2;
int y = elementLoc.getY() + frameLoc.getY() + browserLoc.getY() + elementSize.getHeight()/2;
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.mouseMove(x, y);
Actions action = new Actions(driver);
action.clickAndHold(listElement1);
Action a = action.build();
a.perform();
Thread.sleep(1000);
driver.switchTo().defaultContent();
driver.switchTo().frame(0);
WebElement frame2 = driver.findElement(By.xpath("//iframe[2]"));
Point frameLoc2 = frame2.getLocation();
driver.switchTo().frame(frame2);
WebElement frame2Body = driver.findElement(By.xpath("//body"));
Point frame2BodyLoc = frame2Body.getLocation();
Dimension frame2BodySize = frame2Body.getSize();
int x1 = frame2BodyLoc.getX() + frameLoc2.getX() + browserLoc.getX() + frame2BodySize.getWidth()/2;
int y1 = frame2BodyLoc.getY() + frameLoc2.getY() + browserLoc.getY() + frame2BodySize.getHeight()/2;
robot.setAutoDelay(50);
robot.mouseMove(x1, y1);
action.moveToElement(frame2Body);
action.release();
a = action.build();
a.perform();
I tried doing it as per your feedback, however it's still same the element is getting dragged but it's not dropping it.
– Sameer Malhotra
Jan 2 at 7:57
Yes, try action.click(frame2Body) after action.release()
– sameeksha sahib
Jan 2 at 10:38
I tried that but it didn't worked solution from Uday worked, Thank you very much for your support.
– Sameer Malhotra
Jan 3 at 3:12
add a comment |
I noticed that on running above code, if you move mouse, you can see the dragged element attached to mouse. Try using Robot library and perform this with Robot library mouse actions.
See https://www.guru99.com/using-robot-api-selenium.html
I can't test this on my end as Robot library has bug on mac and i am using mac, if you are using windows, you can try:
Point browserLoc = driver.manage().window().getPosition();
driver.switchTo().frame(0);
WebElement frame1 = driver.findElement(By.xpath("//iframe[1]"));
Point frameLoc = frame1.getLocation();
driver.switchTo().frame(frame1);
Thread.sleep(1000);
WebElement listElement1 = driver.findElement(By.id("dragFrame-2"));
Point elementLoc = listElement1.getLocation();
Dimension elementSize = listElement1.getSize();
int x = elementLoc.getX() + frameLoc.getX() + browserLoc.getX() + elementSize.getWidth()/2;
int y = elementLoc.getY() + frameLoc.getY() + browserLoc.getY() + elementSize.getHeight()/2;
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.mouseMove(x, y);
Actions action = new Actions(driver);
action.clickAndHold(listElement1);
Action a = action.build();
a.perform();
Thread.sleep(1000);
driver.switchTo().defaultContent();
driver.switchTo().frame(0);
WebElement frame2 = driver.findElement(By.xpath("//iframe[2]"));
Point frameLoc2 = frame2.getLocation();
driver.switchTo().frame(frame2);
WebElement frame2Body = driver.findElement(By.xpath("//body"));
Point frame2BodyLoc = frame2Body.getLocation();
Dimension frame2BodySize = frame2Body.getSize();
int x1 = frame2BodyLoc.getX() + frameLoc2.getX() + browserLoc.getX() + frame2BodySize.getWidth()/2;
int y1 = frame2BodyLoc.getY() + frameLoc2.getY() + browserLoc.getY() + frame2BodySize.getHeight()/2;
robot.setAutoDelay(50);
robot.mouseMove(x1, y1);
action.moveToElement(frame2Body);
action.release();
a = action.build();
a.perform();
I tried doing it as per your feedback, however it's still same the element is getting dragged but it's not dropping it.
– Sameer Malhotra
Jan 2 at 7:57
Yes, try action.click(frame2Body) after action.release()
– sameeksha sahib
Jan 2 at 10:38
I tried that but it didn't worked solution from Uday worked, Thank you very much for your support.
– Sameer Malhotra
Jan 3 at 3:12
add a comment |
I noticed that on running above code, if you move mouse, you can see the dragged element attached to mouse. Try using Robot library and perform this with Robot library mouse actions.
See https://www.guru99.com/using-robot-api-selenium.html
I can't test this on my end as Robot library has bug on mac and i am using mac, if you are using windows, you can try:
Point browserLoc = driver.manage().window().getPosition();
driver.switchTo().frame(0);
WebElement frame1 = driver.findElement(By.xpath("//iframe[1]"));
Point frameLoc = frame1.getLocation();
driver.switchTo().frame(frame1);
Thread.sleep(1000);
WebElement listElement1 = driver.findElement(By.id("dragFrame-2"));
Point elementLoc = listElement1.getLocation();
Dimension elementSize = listElement1.getSize();
int x = elementLoc.getX() + frameLoc.getX() + browserLoc.getX() + elementSize.getWidth()/2;
int y = elementLoc.getY() + frameLoc.getY() + browserLoc.getY() + elementSize.getHeight()/2;
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.mouseMove(x, y);
Actions action = new Actions(driver);
action.clickAndHold(listElement1);
Action a = action.build();
a.perform();
Thread.sleep(1000);
driver.switchTo().defaultContent();
driver.switchTo().frame(0);
WebElement frame2 = driver.findElement(By.xpath("//iframe[2]"));
Point frameLoc2 = frame2.getLocation();
driver.switchTo().frame(frame2);
WebElement frame2Body = driver.findElement(By.xpath("//body"));
Point frame2BodyLoc = frame2Body.getLocation();
Dimension frame2BodySize = frame2Body.getSize();
int x1 = frame2BodyLoc.getX() + frameLoc2.getX() + browserLoc.getX() + frame2BodySize.getWidth()/2;
int y1 = frame2BodyLoc.getY() + frameLoc2.getY() + browserLoc.getY() + frame2BodySize.getHeight()/2;
robot.setAutoDelay(50);
robot.mouseMove(x1, y1);
action.moveToElement(frame2Body);
action.release();
a = action.build();
a.perform();
I noticed that on running above code, if you move mouse, you can see the dragged element attached to mouse. Try using Robot library and perform this with Robot library mouse actions.
See https://www.guru99.com/using-robot-api-selenium.html
I can't test this on my end as Robot library has bug on mac and i am using mac, if you are using windows, you can try:
Point browserLoc = driver.manage().window().getPosition();
driver.switchTo().frame(0);
WebElement frame1 = driver.findElement(By.xpath("//iframe[1]"));
Point frameLoc = frame1.getLocation();
driver.switchTo().frame(frame1);
Thread.sleep(1000);
WebElement listElement1 = driver.findElement(By.id("dragFrame-2"));
Point elementLoc = listElement1.getLocation();
Dimension elementSize = listElement1.getSize();
int x = elementLoc.getX() + frameLoc.getX() + browserLoc.getX() + elementSize.getWidth()/2;
int y = elementLoc.getY() + frameLoc.getY() + browserLoc.getY() + elementSize.getHeight()/2;
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.mouseMove(x, y);
Actions action = new Actions(driver);
action.clickAndHold(listElement1);
Action a = action.build();
a.perform();
Thread.sleep(1000);
driver.switchTo().defaultContent();
driver.switchTo().frame(0);
WebElement frame2 = driver.findElement(By.xpath("//iframe[2]"));
Point frameLoc2 = frame2.getLocation();
driver.switchTo().frame(frame2);
WebElement frame2Body = driver.findElement(By.xpath("//body"));
Point frame2BodyLoc = frame2Body.getLocation();
Dimension frame2BodySize = frame2Body.getSize();
int x1 = frame2BodyLoc.getX() + frameLoc2.getX() + browserLoc.getX() + frame2BodySize.getWidth()/2;
int y1 = frame2BodyLoc.getY() + frameLoc2.getY() + browserLoc.getY() + frame2BodySize.getHeight()/2;
robot.setAutoDelay(50);
robot.mouseMove(x1, y1);
action.moveToElement(frame2Body);
action.release();
a = action.build();
a.perform();
answered Jan 2 at 7:05


sameeksha sahibsameeksha sahib
112
112
I tried doing it as per your feedback, however it's still same the element is getting dragged but it's not dropping it.
– Sameer Malhotra
Jan 2 at 7:57
Yes, try action.click(frame2Body) after action.release()
– sameeksha sahib
Jan 2 at 10:38
I tried that but it didn't worked solution from Uday worked, Thank you very much for your support.
– Sameer Malhotra
Jan 3 at 3:12
add a comment |
I tried doing it as per your feedback, however it's still same the element is getting dragged but it's not dropping it.
– Sameer Malhotra
Jan 2 at 7:57
Yes, try action.click(frame2Body) after action.release()
– sameeksha sahib
Jan 2 at 10:38
I tried that but it didn't worked solution from Uday worked, Thank you very much for your support.
– Sameer Malhotra
Jan 3 at 3:12
I tried doing it as per your feedback, however it's still same the element is getting dragged but it's not dropping it.
– Sameer Malhotra
Jan 2 at 7:57
I tried doing it as per your feedback, however it's still same the element is getting dragged but it's not dropping it.
– Sameer Malhotra
Jan 2 at 7:57
Yes, try action.click(frame2Body) after action.release()
– sameeksha sahib
Jan 2 at 10:38
Yes, try action.click(frame2Body) after action.release()
– sameeksha sahib
Jan 2 at 10:38
I tried that but it didn't worked solution from Uday worked, Thank you very much for your support.
– Sameer Malhotra
Jan 3 at 3:12
I tried that but it didn't worked solution from Uday worked, Thank you very much for your support.
– Sameer Malhotra
Jan 3 at 3:12
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53981055%2fis-there-any-way-to-drag-and-drop-an-element-from-one-frame-to-another%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
have you tried driver.dragAndDrop(source, destination) ?
– Dheemanth Bhandarkar
Dec 31 '18 at 8:38
Yes, I've tried that as well
– Sameer Malhotra
Dec 31 '18 at 9:19