power_settings_new

how to drag element using selenium

by (14 points)
asked
0 votes
204 views
I have a scenario, where I need to drag element. how do I implement this?

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

2 Answers

answered by
selected by
0 votes
 
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();

answered by (11 points)
0 votes
-> 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 = "[email protected]";
        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();
        */   
}   
}
add
...