3

I used the below code for drag and drop. It worked in Firefoxdriver but NOT in chromedriver.

WebElement dragElement = driver.findElement(By.id(dragid1));  
WebElement dropElement = driver.findElement(By.id(dropid1));    

Actions builder = new Actions(driver);  
Action drag = builder.clickAndHold(dragElement).build();
drag.perform();

Action move = builder.moveByOffset(355, -20).build();  
move.perform();  
TimeUnit.SECONDS.sleep(2);  
Actions release = builder.clickAndHold(dropElement).release();  
release.perform();   

Please Help!

1
  • What kind of element is that? Is it JQuery sortable? Commented Sep 5, 2014 at 23:05

4 Answers 4

2

If you have both source and target IDs, then why don't you try to use drag and drop?

I'm not very good with Java, but here's how I've done it in Python. I hope it helps you a bit.

from selenium.webdriver.common.action_chains import ActionChains 
actionChains = ActionChains(driver)
actionChains.drag_and_drop(dragElement, dropElement).perform()
Sign up to request clarification or add additional context in comments.

Comments

1

Tried below sample code with chromedriver:2.15, chrome:v43 and is working fine with Chrome.

Sample Code:

    System.setProperty("webdriver.chrome.driver","drivers/chromedriver.exe");
    WebDriver  driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(1,TimeUnit.MINUTES);
    driver.get("http://jqueryui.com/droppable");

    driver.switchTo().frame(0);
    WebElement dragElement = driver.findElement(By.id("draggable"));
    WebElement dropElement = driver.findElement(By.id("droppable"));
    Actions builder = new Actions(driver);
    builder.clickAndHold(dragElement).moveToElement(dropElement).release().build().perform();

Comments

0

Try bundling all those seperate Action objects into a single Actions object

Actions act = new Actions(driver);
act.ClickAndHold(dragElement );
act.MoveToElement(dropElement );
act.Release(dragElement );
act.Build().Perform();

Note: For me, in Chrome & IE, sometimes just dragging to an Element was not enough to make it stick there, and I would have to add in an extra act.MoveByOffset(0, 5); before releasing to move just a few pixels, which seems to work

Is there a reason you have to wait for 2 seconds before releasing, or is that just what worked in FF?

1 Comment

I tried bundling all the separate actions into a SINGLE action. Still didn't work. It didn't work even in Firefox now. I had put the 2 seconds wait, while i was testing the selenium script. It works even without the 2 seconds wait.
0

I had the same problem but got to override it like this:

        //Setup robot
        Robot robot = new Robot();
        robot.setAutoDelay(50);

        //Maximized browser:
        robot.keyPress(KeyEvent.VK_F11);
        Thread.sleep(2000);
        WebElement dragElement = driver.findElement(drag_element);
        Actions builder = new Actions(driver);
        builder.dragAndDropBy(dragElement,0, 200).build().perform();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.