The Actions
class is a collection of individual actions that you want to perform. It is an ability provided by Selenium for handling keyboard and mouse events.
The Actions
class is the user-facing API for emulating complex action events. You can directly use this class rather than using the input devices, i.e. keyboard and mouse.
You can do most of the user interactions like clicking on a button, entering text in textbox using the WebDriver Element Commands such as WebElement.Click()
, and use WebElement.SendKeys() to click on buttons and enter text in text boxes. Submitting a form can be done using the WebElement.Submit() command.
Actions
class in Selenium.Actions
class is useful, especially for mouse and keyboard actions. To perform such actions, Selenium provides different methods for mouse and keyboard actions.
The most popular actions related to mouse events are as follows.
Method | Description |
---|---|
Click() | Clicks on the current mouse location. |
DoubleClick() | Performs double click on the element |
ClickAndHold() | Performs long click on the mouse without releasing it |
DragAndDrop() | Drags the element from one point and drops to another |
MoveToElement() | Shifts the mouse pointer to the center of the element |
ContextClick() | Performs right-click on the mouse |
Release() | Releases the left mouse button at the current mouse location. |
The most popular actions related to keyboard events are as follows.
Method | Description |
---|---|
SendKeys() | Sends a series of keys to the element |
KeyUp() | Performs key release |
KeyDown() | Performs keypress without release |
The following are the most used actions.
The Click()
action method clicks at the current mouse location. It is very useful when combined with MoveToElement()
or MoveByOffset()
methods.
IWebDriver driver = new ChromeDriver();
Actions action = new Actions(driver);
driver.Navigate().GoToUrl("https://www.facebook.com/");
var element = driver.FindElement(By.XPath("//*[@id='reg_pages_msg']/a"));
action.MoveToElement(element).Click();
The ClickAndHold()
action method clicks and holds the mouse button down on the specified element.
IWebDriver driver = new ChromeDriver();
Actions action = new Actions(driver);
driver.Navigate().GoToUrl("https://jqueryui.com/resizable/");
var element = driver.FindElement(By.XPath("//*[@class='ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se']"))
action.ClickAndHold(element).MoveByOffset(200, 200).Release().Perform();
The ContextClick()
action method right-clicks the mouse on the specified element.
// Right click the button to launch right click menu options
Actions action = new Actions(driver);
var element = driver.findElement(By.cssSelector(".context-menu-one"));
action.ContextClick(element).Perform();
The DoubleClick()
action method double-clicks the mouse on the specified element.
Actions action = new Actions(driver);
var element = driver.FindElement(By.xpath("//button[text()='Double-Click Me To See Alert']"));
action.DoubleClick(element).Perform();
The DragAndDrop()
action method performs a drag-and-drop operation from one element to another.
IWebDriver driver = new ChromeDriver();
Actions action = new Actions(driver);
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
driver.Navigate().GoToUrl("https://jqueryui.com/droppable/");
wait.Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.ClassName("demo-frame")));
var source = driver.FindElement(By.Id("draggable"));
var target = driver.FindElement(By.Id("droppable"));
action.DragAndDrop(source, target).Perform();
The KeyDown()
action method sends a modifier key down message to the specified element in the browser.
Actions action = new Actions(driver);
action.KeyDown(Keys.Control).SendKeys(Keys.Subtract).SendKeys(Keys.Subtract).KeyUp(Keys.Control).Perform();
In the above example, it will zoom out the web page using key down Ctrl key and then key up again the Ctrl key.
The KeyUp()
action method sends a modifier key up message to the specified element in the browser.
Actions action = new Actions(driver);
action.KeyDown(Keys.Control).SendKeys(Keys.Subtract).SendKeys(Keys.Subtract).KeyUp(Keys.Control).Perform();
In the above example, it will zoom out the web page using key down Ctrl key and then key up again the Ctrl key.
The MoveByOffset()
action method moves the mouse to the specified offset of the last known mouse coordinates.
IWebDriver driver = new ChromeDriver();
Actions action = new Actions(driver);```
driver.Navigate().GoToUrl("https://jqueryui.com/resizable/");
var element = driver.FindElement(By.XPath("//*[@class='ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se']"))
action.ClickAndHold(element).MoveByOffset(200, 200).Perform();
The MoveToElement()
action method moves the mouse to the specified element.
IWebDriver driver = new ChromeDriver();
Actions action = new Actions(driver);
driver.Navigate().GoToUrl("https://www.facebook.com/");
var element = driver.FindElement(By.XPath("//*[@id='reg_pages_msg']/a"));
action.MoveToElement(element).Click();
The Release()
action method Releases the mouse button on the specified element.
IWebDriver driver = new ChromeDriver();
Actions action = new Actions(driver);
driver.Navigate().GoToUrl("https://jqueryui.com/resizable/");
var element = driver.FindElement(By.XPath("//*[@class='ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se']"))
action.ClickAndHold(element).MoveByOffset(200, 200).Release().Perform();