Action class in Selenium: The Actions class in Selenium helps you perform advanced interactions like mouse movements, clicks, and keyboard inputs that basic WebDriver commands can’t handle.
Syntax:
Actions action = new Actions(driver);
action.moveToElement(element).click().perform();
✓Methods of Action Class
1.Click and Hold(WebElement webelement): click and holds the specified element(any objects).
Syntax:
Actions actions = new Actions(driver);
actions.clickAndHold(targetElement).perform();
2.Click(WebElement webelement): click on the specified element.
Syntax:
Actions actions = new Actions(driver);
actions.click(targetElement).perform();
3.DragAndDrop(WebElement source, WebElement target): Drags an element from a source location to a target location.
Syntax:
Actions actions = new Actions(driver);
actions.doubleClick(targetElement).perform();
4.MoveToElement(WebElement target): Moves the mouse pointer to the center of the specified element.
Syntax:
Actions actions = new Actions(driver);
actions.moveToElement(targetElement).perform();
5.DoubleClick(WebElement webelement): Performs double click on the specified element.
Syntax:
Actions actions = new Actions(driver);
actions.doubleClick(targetElement).perform();
6.ContextClick(WebElement element): Performs a right-click (context click) on the specified element.
Syntax:
Actions actions = new Actions(driver);
actions.contextClick(targetElement).perform();
7.release(): Releases the held mouse button.
Syntax:
Actions actions = new Actions(driver);
actions.release().perform();
8.sendKeys(CharSequence... keysToSend): sends keystrokes to an element
Syntax:
Actions actions = new Actions(driver);
actions.sendKeys(targetElement, "text to send").perform();
9.moveByOffset(int xOffset, int yOffset): Moves the mouse pointer from its current position by the specified offsets.
Syntax:
Actions actions = new Actions(driver);
actions.moveByOffset(100, 50).perform(); // Moves mouse by 100px to the right and 50px down
10.build(): Creates a sequence of actions that you can run later.
Syntax:
Actions actions = new Actions(driver);
Action seriesOfActions = actions.moveToElement(element).click().build();
seriesOfActions.perform();
11.pause(Duration duration):
Adds a delay in the action sequence for a specified amount of time.
Syntax:
Actions actions = new Actions(driver);
actions.moveToElement(element).pause(Duration.ofSeconds(2)).click().perform(); // Hover for 2 seconds then click
Keyboard Actions in Selenium:
1.sendKeys(): Sends a series of keys to the element
Example:
Actions actions = new Actions(driver);
sendKeys(Keys.SPACE)
2.keyUp(): Performs key release
Example:
Actions actions = new Actions(driver);
actions.keyDown(Keys.SHIFT).sendKeys("hello").keyUp(Keys.SHIFT).perform(); // Simulates typing "HELLO"
3.keyDown(): Performs keypress without release.
This is useful for actions like typing in capital letters (with SHIFT) or selecting all text at once (using CTRL + A).
Example:
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform(); // Simulates Ctrl + A
Why we use perform():
• The menu list disappear with in the fractions of seconds before Selenium identify the next submenu item and perform
• click action on it.So,it is better to use perform() method.Only then the desired mouse action can be
performed.
Example code:
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class AdvancedKeyboardActionsExample {
public static void main(String[] args) {
// Set up WebDriver
System.setProperty("webdriver.chrome.driver", "path_to_chromedriver");
WebDriver driver = new ChromeDriver();
// Open a website
driver.get("https://example.com");
// Locate elements
WebElement inputField = driver.findElement(By.id("inputField"));
WebElement textArea = driver.findElement(By.id("textArea"));
// Create Actions object
Actions actions = new Actions(driver);
// Example 1: Type text with SHIFT key
actions
.click(inputField) // Click to focus on the input field
.keyDown(Keys.SHIFT) // Press and hold the SHIFT key
.sendKeys("hello") // Type "hello" with SHIFT pressed (uppercase)
.keyUp(Keys.SHIFT) // Release the SHIFT key
.sendKeys(Keys.SPACE) // Type a space
.sendKeys("world") // Type "world"
.perform(); // Execute the actions
// Example 2: Simulate Ctrl+A (Select All) and Ctrl+C (Copy)
actions
.click(textArea) // Click to focus on the text area
.keyDown(Keys.CONTROL) // Press and hold the CTRL key
.sendKeys("a") // Press 'A' to select all text
.keyUp(Keys.CONTROL) // Release the CTRL key
.sendKeys(Keys.CONTROL, "c") // Press 'CTRL+C' to copy
.perform(); // Execute the actions
// Example 3: Use Tab key to navigate and Enter key to submit
actions
.sendKeys(Keys.TAB) // Navigate to the next element
.sendKeys("Some text") // Type some text
.sendKeys(Keys.ENTER) // Press ENTER to submit
.perform(); // Execute the actions
// Close the browser
driver.quit();
}
}
Common Special Keys:
1.Keys.ENTER: Simulates pressing the Enter key.
2.Keys.TAB: Moves focus to the next element (useful for form navigation).
3.Keys.BACK_SPACE: Simulates pressing the backspace key.
4.Keys.DELETE: Simulates pressing the delete key.
5.Keys.ESCAPE: Simulates pressing the escape key.
6.Keys.ARROW_DOWN: Simulates pressing the down arrow key.
Example of how to perform a mouse hover action on a web element using Selenium in Java:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class MouseHoverExample {
public static void main(String[] args) {
// Set up WebDriver
WebDriver driver = new ChromeDriver();
driver.get("https://example.com"); // Replace with the website URL
// Locate the element to hover over
WebElement hoverElement = driver.findElement(By.id("hoverElementID")); // Replace with the actual element locator
// Create an instance of the Actions class
Actions actions = new Actions(driver);
// Perform the mouse hover action
actions.moveToElement(hoverElement).perform();
// Optional: Perform additional actions after the hover (like clicking a submenu)
WebElement subMenu = driver.findElement(By.id("subMenuID")); // Replace with the submenu element locator
actions.moveToElement(subMenu).click().perform();
// Close the browser
driver.quit();
}
}
What are the difference between Action
vs. Actions
in Selenium?
Action
- Represents a single user interaction (e.g., a click or a keypress).
- It is the result of invoking a method like build() from the Actions class.
Example
Action action = new Actions(driver).moveToElement(element).click().build();
action.perform();
Actions:
- A utility class for creating complex user interactions like drag-and-drop or multiple keypresses.
- Provides methods like clickAndHold(), release(), and moveToElement().
Example
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();
Comments
Post a Comment