1

I tried to drag to drop an element from a frame to a non-frame. I know this is a repetitive question in StackOverflow, but I did not get any correct answer for the same question. Requesting your help on the same.

Code used:

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class DragDropIframes {
 public static void main(String[] args) {
 WebDriver driver = new FirefoxDriver();
 driver.get("http://www.msn.com/en-in/");
 WebDriverWait wait = new WebDriverWait(driver, 30);
 wait.until(ExpectedConditions.presenceOfElementLocated(By.className("adcontainer")));
 WebElement drag = driver.findElement(By.className("adcontainer"));
 WebElement drop = driver.findElement(By.id("q"));
 System.out.println("cde");
 driver.switchTo().frame(0);
 Actions a1 = new Actions(driver);
 Actions a2 = a1.clickAndHold(drag);
 a1.build();
 a2.perform();
 driver.switchTo().defaultContent();
 Actions a3 =a1.moveToElement(drop).release(drop);
 a3.perform();
 }
}

Error message:

Exception in thread "main" org.openqa.selenium.WebDriverException: UnknownError: Cannot release a button when no button is pressed.'UnknownError: Cannot release a button when no button is pressed.' when calling method: [wdIMouse:: up]
Command duration or timeout: 343 milliseconds
Bharat Mane
6,78512 gold badges42 silver badges69 bronze badges
asked Mar 16, 2016 at 14:20

3 Answers 3

1

Selenese is a flavour of Javascript which has a "Same Origin Policy" security restriction. If your frames are from different websites, dragging/dropping between the two would violate the Same Origin Policy and not be allowed.

One answer can be found here.

If you're not married to Selenium, another way to sidestep the Same Origin Policy restriction is to use Sikuli. Sikuli is an open-source java-based cross-platform record/playback tool that uses object recognition instead of object names. If you can see it on the screen, you can automate it. It's not a magic pill that will solve all your automation problems, but it might be helpful here.

answered Nov 16, 2016 at 15:42
0

another way to sidestep the Same Origin Policy restriction is to use Sikuli.

Another object recognition based solution is to use the free Kantu Visual Testing app. It works just like Sikuli, but inside a Chromium web browser. Since it works "inside" Chromium, you can run many tests at once, and run it headless.

answered Oct 16, 2017 at 18:04
0

Irrespective of the structure of frames, one can always use Robot class to drag and drop elements across them. I am providing a sample code just in case:

//Setting up chrome driver
WebDriverManager.getInstance(CHROME).setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
//Redirecting to the website
driver.get("website_url");
Robot robot = new Robot();
robot.mouseMove(x-coordinate1, y-coordinate1);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(2000);
robot.mouseMove(x-coordinate2, y-coordinate2);
Thread.sleep(2000);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
answered Jan 4, 2019 at 2:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.