Rohhit
Rohhit

Reputation: 742

Not able to drag and drop element to another element using Selenium-Webdriver

I am writing demo tests for the following drag and drop functionality.[Refer attached Screenshot] For that I have written following code:

 @Test
    public void DragAndDropTest() {  
      commonSteps();
      WebElement drag = driver.findElement(By.xpath("html/body/div[1]/div[3]/div[2]/div[1]/div[4]/div[3]/div[1]/div[1]/div/div[1]"));
      WebElement drop = driver.findElement(By.xpath("html/body/div[1]/div[3]/div[2]/div[1]/div[4]/div[3]/div[1]/div[2]/div/div[1]"));
      Actions builder = new Actions(driver);
      Action dragAndDrop = builder.clickAndHold(drag).moveToElement(drop).release(drop).build();
      dragAndDrop.perform();
  }

Webelement drag is for "Right Now" & WebElement drop is for "Quick Press".

My code is able to find these elements, but does not drag and drop "Right Now" frame to "Quick Press" frame.

Also I tried to click on drag, but click is also not working on it. I think these are collapsible drag and drop panels of JQuery. So How to handle collapsible drag and drops using webdriver.

What changes should I make in the code to achieve this?

enter image description here

Upvotes: 0

Views: 2850

Answers (3)

Shiva Krishna Chippa
Shiva Krishna Chippa

Reputation: 688

Please try below,

Using Keyboard Actions:

Actions builder = new Actions(driver);
builder.keyDown(Keys.CONTROL).click(someElement).click(someOtherElement). keyUp(Keys.CONTROL).build().perform();

Using Mouse Actions:

Actions builder = new Actions(driver);
builder.clickAndHold(someElement).moveToElement(otherElement).release( otherElement).build().perform();

Upvotes: 0

murali selenium
murali selenium

Reputation: 3927

Instead of click and hold, we can directly use actions to drag and drop.

In Java,

            WebElement drag = driver.findElement(By.xpath("html/body/div[1]/div[3]/div[2]/div[1]/div[4]/div[3]/div[1]/div[1]/div/div[1]"));
      WebElement drop = driver.findElement(By.xpath("html/body/div[1]/div[3]/div[2]/div[1]/div[4]/div[3]/div[1]/div[2]/div/div[1]"));
      Actions builder = new Actions(driver);
      builder.dragAndDrop(drag, drop).build().perform();

      //or we can drop by x and y coordinates 

      builder.dragAndDropBy(drag, 20, 0).build().perform();

Upvotes: 0

Rohhit
Rohhit

Reputation: 742

    Actions builder = new Actions(driver);
    builder.clickAndHold(drag).moveToElement(drop).build();
    builder.dragAndDrop(drag, drop).perform();

This worked for me.

Upvotes: 2

Related Questions