Keyboard Operations using Selenium

Keyboard Events Using Selenium Actions Class API: We use below methods:
sendKeys(keysToSend) : sends a series of keystrokes onto the element.
keyDown(theKey) : Sends a key press without release it. Subsequent actions may assume it as pressed. (example: Keys. ALT, Keys. SHIFT, or Keys. CONTROL)
keyUp(theKey): Performs a key release.

Please refer below Program to understand how to perform keyboard operations:

public class KeyboardMouseActions {

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver;

  System.setProperty("webdriver.chrome.driver","C:\\Users\\Hitendra\\Downloads\\chromedriver_win32\\chromedriver.exe");

  driver = new ChromeDriver();

  driver.manage().window().maximize();

  driver.get("https://www.google.com/");

  Thread.sleep(1000);

  WebElement googleSearch=driver.findElement(By.name("q"));

 
  Actions act=new Actions(driver);

 
  Action action=act.keyDown(googleSearch, Keys.SHIFT)

  .sendKeys("selenium")

  .keyUp(googleSearch, Keys.SHIFT)

  .keyDown(googleSearch, Keys.CONTROL)

  .sendKeys("a")

  .keyDown(googleSearch, Keys.CONTROL)

  .sendKeys("x")

  .keyDown(googleSearch, Keys.CONTROL)

  .sendKeys("v")

  .build();

   action.perform();

                 Thread.sleep(3000);

  driver.close();

 }

}

Please refer below youtube video to understand How to handle Keyboard Operations:


No comments:

Post a Comment