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
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.

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.

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.