How to send keyboard shortcut ALT SHIFT z (hotkey) with Selenium2?


We can send keyboard shortcut ALT SHIFT z(hotkey) with Selenium webdriver. This can be done with the help of the Keys class. We shall use the Keys.chord method and pass Keys.ALT, Keys.SHIFT and z as parameters to that method.

The entire value obtained from the Keys.chord method is obtained as a String. That is then sent as a parameter to the sendKeys method.

Syntax

String s = Keys.chord(Keys.ALT, Keys.SHIFT,"z");
driver.findElement(By.tagName("html")).sendKeys(s);

Example

Code Implementation with Keys.chord method.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class HtKeys{
   public static void main(String[] args) {
         System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("https://www.tutorialspoint.com/index.htm");
      // sending ALT,SHIFT and z by the Keys.Chord
      String s = Keys.chord(Keys.ALT, Keys.SHIFT,"z");
      driver.findElement(By.tagName("html")).sendKeys(s);
      driver.quit();
   }
}

This can also be down with the help of the Actions class. We have to create an object of the Actions class and apply keyUp and keyDown methods on the object and pass Keys.ALT, Keys.SHIFT and z to these methods.

Syntax

Actions a = new Actions(driver);
a.keyDown(Keys.ALT).keyDown(Keys.SHIFT).
sendKeys("z").keyUp(Keys.ALT).keyUp(Keys.SHIFT).build().perform();

Example

Code Implementation with Actions.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Keys;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
public class HotKeyAction{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
      "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      driver.get("https://www.tutorialspoint.com/index.htm");
      // Actions class with keyUp and keyDown methods
      Actions a = new Actions(driver);
      a.keyDown(Keys.ALT).keyDown(Keys.SHIFT).
      sendKeys("z").keyUp(Keys.ALT).keyUp(Keys.SHIFT).build().perform();
      driver.quit();
   }
}

Updated on: 02-Feb-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements