Perform a sequence of actions with Selenium WebDriver

4.6 (91.09%) 119 votes

Problem

We want to perform multiple actions in once, like: drag-and-drop, sliding, selecting multiple items.

Solution

The example code below shows some examples where we can use the Actions interface of Selenium WebDriver.

<code lang="java">
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class ActionExample {
    private static WebDriver driver;

    @BeforeClass
    public void setUp() {
        driver = new FirefoxDriver();
    }

    @AfterClass
    public void tearDown() {
        driver.close();
        driver.quit();
    }

    @Test
    public void draggable() {
        driver.get("http://jqueryui.com/demos/draggable/");

        WebElement draggable = driver.findElement(By.id("draggable"));
        new Actions(driver).dragAndDropBy(draggable, 120, 120).build()
                .perform();
    }

    @Test
    public void droppable() {
        driver.get("http://jqueryui.com/demos/droppable/");

        WebElement draggable = driver.findElement(By.id("draggable"));
        WebElement droppable = driver.findElement(By.id("droppable"));
        new Actions(driver).dragAndDrop(draggable, droppable).build().perform();
    }

    @Test
    public void selectMultiple() throws InterruptedException {
        driver.get("http://jqueryui.com/demos/selectable/");

        List&lt;WebElement> listItems = driver.findElements(By
                .cssSelector("ol#selectable *"));

        Actions builder = new Actions(driver);
        builder.clickAndHold(listItems.get(1)).clickAndHold(listItems.get(2))
                .click();

        Action selectMultiple = builder.build();
        selectMultiple.perform();
    }

    @Test
    public void sliding() {
        driver.get("http://jqueryui.com/demos/slider/");

        WebElement draggable = driver.findElement(By
                .className("ui-slider-handle"));
        new Actions(driver).dragAndDropBy(draggable, 120, 0).build().perform();
    }
}
</code>

3 thoughts on “Perform a sequence of actions with Selenium WebDriver”

  1. hi i have one question using selenium web driver how to select buttons in sequence vice means i have 10 buttons in list with same name how to select those buttons one by one

    1. get all the buttons using xpath = //***button
      Save to a List using findElements(by.xpath(“”))
      Iterate and click based on the total size of list

  2. hello ,
    Getting error at line:
    builder.clickAndHold((listItems.get(1)).clickAndHold(listItems.get(2)).click();
    Error is multiple markers at the line. List item can not be resolved.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.