Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
422 views
in Selenium by Contributing Tester (14 points)
retagged by
I have a scenario, where I need to drag element. how do I implement this?

2 Answers

0 like 0 dislike
by
selected by
 
Best answer

You can use below code:

 

Actions moveSlider = new Actions(driver);
Action action = moveSlider.dragAndDropBy(driver.findElement("Selemnt selector"), xAxis yAxis).build();
action.perform();

0 like 0 dislike
by New User (11 points)
-> go to url -> create action class -> web element source -> web element destination -> perform action (click and hold source -> move to destination element -> release the element)

----

public class DragAndDrop
{
public static void main (String []arg) throws Exception
   
    {
        String url = "https://softwaretestingboard.com/q2a/1/how-to-drag-element-using-selenium#axzz5eTh5jQe0";
        String un ="Admin";
        String pw = "admin@";
        System.setProperty("webdriver.gecko.driver", "D:\\Soft\\geckodriver.exe");
        WebDriver driver =new FirefoxDriver();
        driver.navigate().to(url);
        Thread.sleep(5000);
        Actions ActionDrageAndDrop = new Actions (driver);
        WebElement Source = driver.findElement(By.xpath("/html/body/div[1]/div[2]/ul/a"));
        WebElement Destination = driver.findElement(By.xpath("/html/body/div[1]/div[2]/ul/a"));
        ActionDrageAndDrop.clickAndHold(Source).build().perform();
        ActionDrageAndDrop.moveToElement(Destination).build().perform();
        ActionDrageAndDrop.release(Destination).build().perform();
       
       
        /* also try with direct method dragAndDrop
        ActionDrageAndDrop.dragAndDrop(Source, Destination).build().perform();
        */   
}   
}


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...