3

So I am trying to automated a list element that can be clicked, and dragged to different portions of the ol elements, and then saved. But the test will oly go as far as to hold the element. it will not move by offset, and it will not move to the target element.

Chrome webdriver, Java/Selenium

public void clickAndDragListElement() {
    Actions hold = new Actions(driver);
    hold.clickAndHold(targetHoldElement)
        .moveToElement(targetDestinationElement)
        .release(targetHoldElement)
        .build()
        .perform();
}

(WebElements are defined outside the element)

4 Answers 4

5
new Actions(driver)
                .moveToElement(source)
                .pause(Duration.ofSeconds(1))
                .clickAndHold(source)
                .pause(Duration.ofSeconds(1))
                .moveByOffset(1, 0)
                .moveToElement(destination)
                .moveByOffset(1, 0)
                .pause(Duration.ofSeconds(1))
                .release().perform();
Sign up to request clarification or add additional context in comments.

2 Comments

I think I click the checkmark - used stack overflow for 2 years now, first post ever lol.
Apparently anything other than buttonUp after buttonDown is undefined behaviour. However this trick worked for me!
0

Have you tried something like this:

// Create object of actions class
Actions act=new Actions(driver);

// find element which we need to drag
WebElement drag=driver.findElement(By.xpath(".//*[@id='draggable']"));

// find element which we need to drop
WebElement drop=driver.findElement(By.xpath(".//*[@id='droppable']"));

// this will drag element to destination
act.dragAndDrop(drag, drop).build().perform();

Comments

0

I have tried this and it works perfectly for me:

public class DragAndDrop {

    public static void main(String[] args) {
         System.setProperty("webdriver.chrome.driver", "C:\\Users\\Ranosys\\workspace\\MyTest\\chromedriver.exe");
         WebDriver driver = new ChromeDriver();
         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
          WebDriverWait wait=new WebDriverWait(driver,50 );

         driver.manage().window().maximize();
         driver.get("http://demo.guru99.com/test/drag_drop.html");
       //Element which needs to drag.           
        WebElement From=driver.findElement(By.xpath("//*[@id='credit2']/a"));   

      //Element on which need to drop.      
      WebElement To=driver.findElement(By.xpath("//*[@id='bank']/li"));                 

      //Using Action class for drag and drop.       
      Actions act=new Actions(driver);                  

    //Dragged and dropped.      
      act.dragAndDrop(From, To).build().perform();  


    }

}

Comments

0

None of these solutions worked for me. @Fenio's suggestion seems like the greatest hope for my use case but I got no luck. Instead I decided to use selenium to get the coords of the elements and then use pyautogui (which internally uses Xlib) to do the clicking and mouse movements while the non-headless webdriver is running in kiosk mode. This roundabout solution worked for me.

(I'm using selenium for python btw)

Comments

Your Answer

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.