Selenium Actions Driver Explained Simply (Java)
When do we use the Actions Class?
If you read our article on click commands, you will have seen the usage of the Actions driver to perform click commands beyond a simple left-click.
The Actions Class can do far more than non-standard clicks. They are generally used whenever you want to emulate a user's interactions with the page beyond sending keys to or clicking on an element. For example, you can use Actions to move to elements, click drag them around the screen, or hold keys down while performing other actions.
Besides the niche methods provided by the Actions Class, the other main usage is creating chains of actions. Using Actions, you can combine multiple different actions in the same command. For example, you could combine holding down the control key with clicking a link to open it in a new tab:
WebElement searchConfirmButton = driver.findElement(By.xpath("//fieldset//button")); Actions actionsDriver = new Actions(driver); actionsDriver.keyDown(Keys.CONTROL).click(searchConfirmButton).keyUp(Keys.CONTROL).build().perform();
Note: Whenever you use an Actions method, always make sure to add the methods build() and perform() to the end, or your test will not perform the action you specified.
This example has a runnable case associated with it. The case contains the code required to switch to the new tab and the pause afterwards to show the result in the live video feed. If you want to see the case being run, click here:
Other methods that can used with the Actions class.
Hovering over elements
A very common use of Actions is to force the driver to hover its mouse over an element to trigger an onHover event. For example:
WebElement languageLinkEnglish = driver.findElement(By.xpath("//a[@id='js-link-box-en']//strong")); Actions actionsDriver = new Actions(driver); actionsDriver.moveToElement(languageLinkEnglish).build().perform();
This example has a runnable case associated with it. The case will hover the english language link on the wikipedia language selection page and check that the button turns grey. There is a pause, so you can see what the case has done in the live video.
If you want to see the case being run, click here:
Sometimes when testing, you will come across click intercepted exceptions due to elements covering the ones you want to click on. In a lot of these cases, a simple change to your location strategy can be all that is needed, but sometimes it is easier to just use the Actions class to move to the desired element and then send a click command.
WebElement languageLinkEnglish = driver.findElement(By.xpath("//a[@id='js-link-box-en']//strong")); Actions actionsDriver = new Actions(driver); actionsDriver.moveToElement(languageLinkEnglish).click().build().perform();
This results in clicking the language option without the WebDriver needing to check if it is actually clickable. As the result of this action is predictable and simple, there is no runnable case associated with it.
To see the full list of methods available to the Actions class, follow this link to the API reference:
Leave a reply
Your email address will not be published. Required fields are marked*