Tuesday, 10 April 2012

Key combinations in Selenium 1 & 2

In this article, I would like to share my learning related to emulation of key combination press actions in Selenium 1 & 2. In our day to day activities, we would use the modifier keys number of times while working with the application. We would use ‘Alt+F4’ key to close the window, ‘Ctrl+C’ to copy the contents and several other key combinations for various activities.

In the aforesaid key combination (say for example-Ctrl+C), first part (Ctrl) of the combination is called as modifier key – as this key modifies the intended function of the second part of the key combination.

In the leading commercial test automation tools such as QTP and Silktest – required combination of key strokes can be easily achieved with the use of designated methods such as ‘type’ method and by using the appropriate constant that corresponds to the individual keys.

But in Selenium, using these key combination requires some better understanding about the intricacies of Selenium. So, in this article – I would explain about how to use these key combinations in ‘Selenium 1 & 2’.

In Selenium 1 – we can simulate the key combination action in a relatively simple way by using the following methods: KeyPress, KeyUp and KeyDown.

We just need to use the above mentioned methods in the instance of ‘DefaultSelenium’ object, with the appropriate ascii value of the key.

In case of more than 2 key combinations, such as ‘Shift+Alt+T’ - we can use the KeyDown and KeyUp methods to achieve the required key combinations, as below:

DefaultSelenium.KeyDown(<asciivalue of the key1>);
DefaultSelenium.KeyDown(<asciivalue of the key2>);
DefaultSelenium.KeyPress(<asciivalue of the key3>);

You can refer the below mentioned link to find the ascii value of various keys:  http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000520.html

In Selenium 2 – to simulate the above mentioned key combinations, we would be required to use the ‘Actions’ class of Selenium Webdriver as below:

FirefoxDriver driver = new FirefoxDriver();           
Actions kpress = new Actions(driver);
kpress.keyDown(driver.findElement(By.id("name")), Keys.ALT).perform();
kpress.keyDown(driver.findElement(By.id("name")), Keys.SHIFT).perform();
kpress.sendKeys(driver.findElement(By.id("name")), "T").perform();
kpress.keyUp(driver.findElement(By.id("name")),Keys.SHIFT).perform();              kpress.keyUp(driver.findElement(By.id("name")),Keys.SHIFT).perform();

Here the objective is to simulate the press action of the key combination ‘Alt+Shift+T’. To perform this action, we are using the keyDown and SendKeys method of Selenium Action class.

I would post another article about F4 keys usage and action builder quite soon. If you have any questions in the above mentioned concepts, please let me know.


  1. i get list of my results in ajax autocomplete dropdown whose id is dynamic. I can't enter that box even because of dynamic id and so xpath. How could I select one of the result using keydown action in webdriver.
    Scenario is I enter one string in input box and possible results appears, i have to select one of the result.Please Help me out.
    contact: shekhar90907@gmail.com

  2. Hi Chandra,

    I would suggest you to approach this scenario by dynamically capturing the dropdowns present in the webpage...if you use d1.findelementsByTagname("select") - it would return you the list of dropdowns present in the web page...then either based on the index of the dropdown/some property value - access the dropdown...

    Pls let me know if this suggestion is of any help...