How to handle Action class in Selenium
To test an application, one needs to perform a number of user actions on it. To perform any operations on the web application such as double-click, selecting drop-down boxes, etc. the actions class is required. This article discusses how to handle the action class in Selenium.
Table of ContentsWhat is Action Class in Selenium?
Actions class is an ability provided by Selenium for handling keyboard and mouse events. In Selenium WebDriver, handling these events includes operations such as drag and drop in Selenium, clicking on multiple elements with the control key, among others. These operations are performed using the advanced user interactions API. It mainly consists of Actions that are needed while performing these operations.
Action class is defined and invoked using the following syntax:
Actions action = new Actions(driver); action.moveToElement(element).click().perform();
Methods of Action Class
Action class is useful mainly for mouse and keyboard actions. In order to perform such actions, Selenium provides various methods.
Mouse Actions in Selenium:
Keyboard Actions in Selenium:
Now, let’s understand how to perform various mouse and keyboard actions.
Learn the fundamentals of Selenium coding with the help of Selenium Commands.
Examples of Action Class in Selenium
1. Perform Click Action on the Web Element
Test Scenario: Visit the Browserstack home page and click on the Get Started Free button.
Code Snippet:
driver.get("https://www.browserstack.com/"); Actions action = new Actions(driver); element = driver.findElement(By.linkText("Get started free")); action.moveToElement(element).click(); //using click action method
Follow-up Read: Understanding Click Command in Selenium
2. Perform Mouse Hover Action on the Web Element
Test Scenario: Perform Mouse Hover on Live Tab and App Automate Tab on the Browserstack Website.
Code Snippet:
import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; public class Mouse { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver_win32\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://www.browserstack.com/"); ((JavascriptExecutor) driver).executeScript("scroll(0,300)"); Actions ac = new Actions(driver); WebElement live= driver.findElement(By. cssSelector("div.product-cards-wrapper--click a[title='Live']")); ac.moveToElement(live).build().perform(); Thread.sleep(3000); WebElement automate= driver.findElement(By.cssSelector("div.product-cards-wrapper--click a[title='App Automate']")); automate.click(); Thread.sleep(2000); //Thread.sleep(4000); driver.quit(); } }
The code above will perform the mouse hover using Selenium on the Live tab and then move to the App Automate tab and click.
3. Perform Double Click Action on the Web Element
Test Scenario: Perform Double Click Action on Free Trial Button in the Browserstack Home page.
Code Snippet:
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 Mouse { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver_win32\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://www.browserstack.com/"); Actions a = new Actions(driver); //Double click on element WebElement trialaction = driver.findElement(By.xpath("//a[@id='free-trial-link-anchor']")); a.doubleClick(trialaction).perform(); } }
In the code above, the Action class is created to perform the double click action on the element named Free Trial.
Want to try executing the above code on real browsers and devices on the cloud?
Learn how to perform various actions on Web Elements using Locators in Selenium.
Note: The Selenium Actions class is useful for performing actions on any element on the screen by specifying x and y coordinates. It is possible to locate more than one web element using the Actions class.
Using Action class in Selenium is of utmost importance in automated testing. This article simplifies the process so that testers know how to simulate common user actions on websites and applications. This lets them monitor software behavior in the real user conditions so that they can verify and optimize user experience for its best possible state.
ncG1vNJzZmivp6x7o77OsKqeqqOprqS3jZympmeXqralsY6amq2hn6N6pLjArKpmoZ5iwKa4xKegrqU%3D