Professional Documents
Culture Documents
66 Selenium Webdriver - Handle Special Keys
66 Selenium Webdriver - Handle Special Keys
Selenium Webdriver can be used to handle special keys while creating the automation tests. This
is done using the Actions class and sendKeys() method in Selenium. The operations like key
up/down using the keyUp() and keyDown() methods are mostly used to work with special keys. In
case we are using sendKeys() method we would need to pass Key.chord as a parameter to this
method.
Please note that, while using the methods of Actions class, we would need to add the below
import statement in our test −
import org.openqa.selenium.interactions.Actions.
Example
package org.example;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;
// Closing browser
driver.quit();
}
}
Output
In the above example, we had first entered the text Selenium in the first input box and then copied
and pasted the same text in the second input box using the methods of the Actions class. Finally,
we had obtained the entered text in the second input box as a message in the console - Value
copied and pasted: Selenium.
Finally, the message Process finished with exit code 0 was received, signifying successful
execution of the code.
package org.example;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
driver.quit();
}
}
Output
In the above example, we had first entered the text Selenium in the first input box and then copied
and pasted the same text in the second input box using the sendKeys() and Key.chord() methods.
Finally, we had obtained the entered text in the second input box as a message in the console -
Value copied and pasted: Selenium.
Finally, the message Process finished with exit code 0 was received, signifying successful
execution of the code.
Example
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;
// Actions class
Actions a = new Actions(driver);
// moving to an input box and clicking on it
a.moveToElement(e).click();
// Closing browser
driver.quit();
}
}
Output
In the above example, we had entered the text selenium along with pressing SHIFT key in the
input box and also obtained the entered text in upper case with the message in the console - Text
entered: SELENIUM.
Finally, the message Process finished with exit code 0 was received, signifying successful
execution of the code.
This concludes our comprehensive take on the tutorial on Selenium Webdriver- Handle Special
Keys. We’ve started with describing an example of copying and pasting text taking help of special
keys like CONTROL, SHIFT, TAB, CONTROL + A, CONTROL + V, CONTROL + C, and so on along
with and without the Actions class, and illustrating how to input text in upper case with Selenium.
This equips you with in-depth knowledge of handling special keys in Selenium Webdriver. It is
wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen
your understanding and expand your horizons.