Selenium · Browser automation foundations
How does the Selenium Actions API work?
Short answer
Actions builds a sequence of low-level input commands such as move, click, drag and key presses, then performs them as one W3C actions payload. It is required for hover, drag and drop, and multi-step input.
A normal click is a single command. Actions lets you compose pointer and keyboard input, optionally with pauses. The commands are buffered until perform is called, and the browser executes the sequence in order.
Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.id("menu"))).perform();
actions.moveToElement(driver.findElement(By.linkText("Reports"))).click().perform();
WebElement card = driver.findElement(By.id("card"));
WebElement column = driver.findElement(By.id("done"));
actions.dragAndDrop(card, column).perform();
actions.moveToElement(card).contextClick().perform();- moveToElement triggers hover states and submenus
- dragAndDrop performs click, move and release as one sequence
- clickAndHold, moveByOffset and release give finer control for custom drag implementations
- contextClick opens a context menu; the menu itself is then located normally
- pause inserts a delay inside the action chain when the UI needs a beat between steps
- Always call perform; building the chain alone does nothing
Native HTML5 drag and drop can be unreliable because some implementations rely on dataTransfer events that synthetic pointer actions do not fully reproduce. If dragAndDrop fails consistently, check whether the component exposes a test hook or whether the application supports a click-based fallback.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why does building an Actions chain without perform do nothing?
- 2How do you drag to an offset rather than an element?
- 3Why can HTML5 drag and drop be unreliable?
- 4When would you use pause inside an action chain?
Say it in the interview
Actions composes pointer and keyboard steps and sends them as one W3C input sequence when perform is called. I use it for hover menus, drag and drop and right-click, then locate any resulting menu normally. For custom drag implementations I use clickAndHold, moveByOffset and release. I avoid long pauses and instead wait for the state the action should produce.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Selenium mock interview and get scored feedback on depth, clarity and confidence.