How to press shift + ctrl + s in Selenium

Viewed 28855

How to press shift + ctrl + s in Selenium ? I have used the code below:

Actions action = new Actions(driver);   
action.sendKeys(Keys.chord(Keys.SHIFT + Keys.CONTROL + "s")).perform();

its Throwing error

1 Answers

If you simply send a series of keys, then Webdriver for each keycode first press a given key, then depress it.
So your code sendKeys(Keys.chord(Keys.SHIFT + Keys.CONTROL + "s") is equivalent to the below series of events in the time:

  1. Press SHIFT
  2. Depress SHIFT
  3. Press CONTROL
  4. Depress CONTROL
  5. Press s
  6. Depress s

This is not what you want, because you are excpecting that Ctrl and Shift have been pressed and are held at the moment of time when the S key is pressed.


You need to use Actions#keyDown method to press the key and leave it in the pressed state, and later Actions#keyUp to release the key. So the sequence of actions might be:

  1. Press SHIFT - using keyDown
  2. Press Ctrl - using keyDown
  3. Press then release S (this key can be pressed and immediately released using sendKeys method)
  4. Wait for an visible effect of pressing Ctrl-Shift-S
  5. Release Ctrl - using keyUp
  6. Release Shift - using keyUp

Points 5 and 6 (releasing keys) must be done in order to avoid unexpected effects later in the test code (don't leave Ctrl+Shift in a pressed state).


Here is a link to simple page on jsfiddle which help us to test our WebDriver code.

<body>

<p>Press a key on the keyboard in the input field to find out if the Ctrl-SHIFT key was pressed or not.</p>

<input id="ctrl_shift_s" type="text" onkeydown="isKeyPressed(event)">

<p id="demo"></p>

<script>
function isKeyPressed(event) {
    console.log( event.keyCode);
    var x = document.getElementById("demo");
    if (event.shiftKey && event.ctrlKey && event.keyCode == 83 ) {
        x.innerHTML = "The Ctrl-SHIFT-S keys were pressed!";
    } else {
        x.innerHTML = "Please press Ctrl-SHIFT-S";
    }
}
</script>

</body>

If you move a cursor to INPUT field on this page (id="ctrl_shift_s" of this element), and then press Ctrl-SHIFT-S keys (holding SHIFT and Ctrl), then a message will appear The Ctrl-SHIFT-S keys were pressed! enter image description here


Below is an example (working) code tested agaist the above test page using latest IE,Firefox and Chrome drivers. You must use requireWindowFocus(); option in order to run Actions in IE driver.

WebDriver driver= null;
try{

    System.setProperty("webdriver.ie.driver", "C:\\instalki\\IEDriverServer.exe");
    System.setProperty("webdriver.chrome.driver", "C:\\instalki\\chromedriver.exe");
    System.setProperty("webdriver.gecko.driver", "C:\\instalki\\geckodriver.exe");

    InternetExplorerOptions opt =  new InternetExplorerOptions();
                opt.requireWindowFocus();
    //          driver=new InternetExplorerDriver(opt);
    //          driver = new ChromeDriver();
    driver = new FirefoxDriver();

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

   WebDriverWait wait = new WebDriverWait( driver, 10);

   driver.get("https://jsfiddle.net/39850x27/2/");
   final By inputField = By.id("ctrl_shift_s");
   final By messageWeWaitFor = By.xpath("//*[text() = 'The Ctrl-SHIFT-S keys were pressed!' ]");
   final By frame = By.name("result");

   // Swift to a frame (our test page is within this frame)
   driver.switchTo().frame(driver.findElement(frame));

   // move a corsor to the field
   wait.until(ExpectedConditions.elementToBeClickable(inputField)).click();

   Actions a = new Actions(driver);

   // Press SHIFT-CTRL-S            
   a.keyDown(Keys.SHIFT)
    .keyDown(Keys.CONTROL)
    .sendKeys("s")
    .build()
    .perform();


  //Wait for a message
 wait.until(ExpectedConditions.visibilityOfElementLocated(messageWeWaitFor));

   System.err.println("Success - Ctrl-Shift-S were pressed !!!");

   // Sleep some time (to see the message is really on the page)        
   Thread.sleep(5000);

   // Release SHIFT+CTRL keys   
   a.keyUp(Keys.CONTROL)
    .keyUp(Keys.SHIFT)
    .build()
    .perform();

}finally {
    if(driver!=null) {
        driver.quit();
    }
}
Related