Perform series of actions using Actions class in selenium WebDriver?

You can build a perform series of actions using the Action and Actions classes. Just remember to close the series with the build() method. Consider the sample code below.

Perform Series of Actions Using Actions class in selenium WebDriver
package learnAboutActionsClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class PerformSeriesOfActions {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  WebDriver driver = new FirefoxDriver();
  
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();
  
  driver.navigate().to("https://www.facebook.com/");
  
  WebElement element = driver.findElement(By.xpath("//*[@id='email']"));
  
  Actions act = new Actions(driver);
  //Below line sends some text to field by converting it to uppercase, then double click the text so that it will select all, then do right click
  act.moveToElement(element).click().keyDown(element, Keys.SHIFT).sendKeys("panarkhede").keyUp(element, Keys.SHIFT).doubleClick(element).contextClick(element).build().perform();
  driver.quit();
 }

}
Observe below screen shot to see the series of actions performed by using above code –
Firstly, Clicked on text box.
then, sent text to text box by converting it to upper case.
then double clicked on the entered test, so that text is selected.
then right clicked on the text.

Hope This Helps !!!

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories