Mouse Actions in Selenium WebDriver
In this Selenium tutorial, we will be studying the advanced mouse interactions using Actions class. Using these methods, we can perform mouse operations like right-click, double click, mouse hover, click and hold, etc.
Content
Actions Class Method for Mouse Interactions
- click()-
This method is used to click at the current mouse pointer position. It is particularly useful when used with other mouse and keyboard events, generating composite actions. - click(WebElement webElement)-
This method is used to click at the middle of a web element passed as parameter to the click() method. - clickAndHold()-
The clickAndHold() method is used to perform the click method without releasing the mouse button. - clickAndHold(WebElement onElement)-
This method performs the click method without releasing the mouse button over a web element. - contextClick()-
This method is used to perform the right click operation(context-click) at the current mouse position. - contextClick(WebElement onElement)-
This method performs the right click operation at a particular web element. - doubleClick()-
As the name suggest, this method performs double click operation at a current mouse position. - doubleClick(WebElement onElement)-
Performs the double click operation at a particular web element. - dragAndDrop(WebElement fromElement, WebElement toElement)-
This is a utility method to perform the dragAndDrop operation directly wherein, we can pass the source element and the target element as parameter. - dragAndDropBy(WebElement fromElement, int xOffset, int yOffset)-
This method is a variation of dragAndDrop(fromElement, toElement) in which instead of passing the target element as parameter, we pass the x and y offsets. The method clicks the source web element and then releases at the x and y offsets. - moveByOffset(int xOffset, int yOffset)-
This method is used to move the mouse pointer to a particular position based on the x and y offsets passed as parameter. - moveToElement(WebElement toElement)-
This method is used to move the mouse pointer to a web element passed as parameter. - moveToElement(WebElement toElement, int xOffset, int yOffset)-
This method moves the mouse pointer by the given x and y offsets from the top-left corner of the specified web element. - release()-
This method releases the pressed left mouse button at the current mouse pointer position. - release(WebElement onElement)-
This method release the pressed left mouse button at a particular web element.
Code snippet for a Mouse Action
//WebElement which needs to be right clicked
WebElement rtClickElement = driver.findElement(By Locator of rtClickElement);
//Generating a Action to perform context click or right click
Actions rightClickAction = new Actions(driver).contextClick(rtClickElement);
//Performing the right click Action generated
rightClickAction.build().perform();
This completes our tutorial on Mouse Actions in Selenium WebDriver. For complete step by step Selenium WebDriver tutorial check – Selenium Tutorial.