KeyBoard Events in Selenium:
- When selenium Webdriver is used, browser controls and application types of the WebDriver by reference variables, meaning that the WebDriver reference variable can identify any web element on the page.
- But it does not have the ability to handle all keyboard and mouse events such as right-click, drag and drop, clickAndHold etc.
Ways To Handle Keyboard Keys:
- Handle Keyboard Keys using Action class.
- Handle Keyboard Keys using sendkeys chord.
- Handle Keyboard keys using Robot class.
1. Handle Keyboard Keys using Action class:
Actions Class Method for Keyboard:
keyDown and keyUp are the main methods in KeyBoard Events in Selenium Webdriver Actions class.
- public Actions keyDown(Keys theKey) : Performs a modifier key press (SHIFT,Keys.ALT or Keys.CONTROL) to Handle keyDown operation.
- public Actions keyDown(WebElement element, Keys theKey) : Performs a modifier key press (SHIFT,Keys.ALT or Keys.CONTROL) after focusing on an element perform keyDown using WebElement.
- public Actions keyUp(Keys theKey) : Performs a modifier key release (SHIFT,Keys.ALT or Keys.CONTROL) to Handle keyUp operation.
- public Actions keyUp(WebElement element, Keys theKey) : performs a modifier key release after focusing on an element to perform keyUp operation.
- public Actions sendKeys(java.lang.CharSequence… keysToSend) : The key sends the active element to the key, it is actively different from calling sendKeys two passes (CharSequence…) on an active element in two ways: Modifiers are not included in this call, and no one is able to focus the element again. Do not try. Then we will send some sendKeys(Keys.TAB) to switch the elements.
- public Actions sendKeys(WebElement element, java.lang.CharSequence… keysToSend) : Sends keys to the given element using sendKeys.
Handle Single Keyboard Key using Action class:
- Press ENTER using sendKeys with the help of Action Class.
1 2 3 | Actions action = new Actions(driver); action.sendKeys(Keys.ENTER).build().perform(); |
Handle multiple Keyboard keys using Actions class:
1 2 3 | Actions action = new Actions(driver); action.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform(); |
Example: Handle Single / Multiple Keys using Action class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class KeyBoard_Action_Events { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Create object of Action class Actions action = new Actions(driver); // Sendkeys using Action class object action.sendKeys(Keys.ENTER).build().perform(); // Send mulyiple keys using action class action.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform(); } } |
2. Handle Keyboard Keys using sendkeys chord:
Handle Single Keyboard Key using Sendkeys:
1 | driver.findElement(By.xpath(“ele_path”)).sendKeys(Keys.ENTER); |
Handle multiple Keyboard keys using chord:
- When we want to Press multiple keys that time we use chord.
1 | driver.findElement(By.xpath("//body")).sendKeys(Keys.chord(Keys.CONTROL, "a")); |
Example: Handle Single / Multiple Keys using Sendkeys / Chord:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class KeyBoard_Sendkeys_Events { public static void main(String[] args) { System.setProperty("webdriver.firefox.marionette", "C:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Send single key using sendkeys driver.findElement(By.xpath("ele_path")).sendKeys(Keys.ENTER); // Handle multiple keys using chord driver.findElement(By.xpath("//body")).sendKeys(Keys.chord(Keys.CONTROL, "a")); } } |
3. Handle Keyboard keys using Robot class.
Handle Keyboard keys using Robot class:
- In Robot Class using keyPress Method we handle KeyBoard events.
1 2 3 4 5 6 7 8 | // Create Robot class Robot rb = new Robot(); // Press control keyboard key rb.keyPress(KeyEvent.VK_CONTROL); // Press A keyboard key rb.keyPress(KeyEvent.VK_A); |
Example of Handle Keyboard keys using Robot class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.awt.Robot; import java.awt.event.KeyEvent; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class KeyBoard_Robot_Events { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Create Robot class Robot rb = new Robot(); // Press control keyboard key rb.keyPress(KeyEvent.VK_CONTROL); // Press A keyboard key rb.keyPress(KeyEvent.VK_A); } } |
Selenium Keyboard Events Example and Handling Keyboard Actions in Selenium WebDriver and Handle Keyboard Keys using Action class and Handle Keyboard Keys using sendkeys