Updated April 3, 2023
Introduction to Selenium Keys
The selenium keys and the web drivers will do enter or press the keys example ctrl+A keyboard option and it can be accomplished in a different ways for to imitate the keyboard operation and it can be utilised using the option like Keys.chord() and other default method for makes it possible to press and enter the multiple set of keys at the same time this method will accept the series keys and it passes the argument using the string format datatype by the default setting using the keyboard option press and enter the keys like ctrl+a , down, up arrow keys.
Selenium Keys Overview
- The selenium has n number of keys that will be used in the different areas of automation testing to perform the user operations.
- The keys provided with different types like enum, string, integer etc these are some default keys which provided from the user end and other web elements, drivers will support the datatypes.
- Keys that can be pressed but are not text keys for sending to the browsers are represented by these symbols.
How to Use Selenium Keys?
- In Selenium, we can use the Enter/Return key as general purpose that is we can access the application through the automation this keys are basically identified the datas and inputs. Then we will utilise the sendKeys() method and supply the argument Keys as the parameter.
- We can also use ENTER() to the method for the same purpose, but it pass RETURN as an argument to the sendKeys() method. Import org must be included in the selenium code it must be order to use the Keys class. First it must be include import org.openqa.selenium.Keys in the code then we use the Keys class. The user inputs should be passed as the keys like Enter/Return after entering text into the edit box on the UI framework.
- Not only we used keyboard and mouse events we can use selenium Actions class. We can hold a key and down with the selenium webdriver it will utilize the control/shift/alt keys to hold the specific operations in down and then we can access and able to click the other keys. So that we can sue and modified the keys like control/keys.
Special Keys
- It has n number of keys for each set of keys have different operations and send the keys without specifying the web elements in the selenium web driver. Generally, the tag elements and tagname inputs are used for to edit the inputs an UI boxes elements.
- We can also use the method called find_element_by_tag_name() for passing the input parameters to the method.
- The special keys are the exclusive feature of selenium that used for allows and press the keys through via keyboards like ctrl+f, shift+c+v these are the some general keys that associated with the class called selenium.webdriver.common.keys. It is also associated with the other keys and it generates special key codes for each set of key and it is a unique one for generate the code points.
Enum Keys
- The enum keys are the general representations of the keys that can be pressed against the keys that are not associated with the text. It is stored as the Unicode format and assigned called to the special key codes for each set of keys.
- The Return and Enter keys are the general special keys that can be associated with the other keys with generic unique codes the web element will generate and call the default methods for each set of operations.
- Hence there is no functional and operational difference while working with the another set of keyboard events. We can use other keys like Arrow keys and down keys, Control, Alt, Delete, Enter, Home, F1, F2, F10, F11, F12 these keys are summarized with the different set of drivers that applicable for each web elements.
Examples of Selenium Keys
Given below are the examples of Selenium Keys:
Example #1
Code:
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;
public class New {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Kripya-PC\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe");
WebDriver dr = new ChromeDriver();
dr.manage().window().maximize();
dr.get("https://in.search.yahoo.com/?fr2=inr");
WebElement st1 = dr.findElement(By.linkText("Sign In"));
Actions act= new Actions(dr);
act.moveToElement(st1).keyDown(Keys.CONTROL).click().keyUp(Keys.CONTROL).build().perform();
}
}
Output:
In the above example we used some basic imports like By, Keys, WebDriver, WebElement, ChromeDriver, Actions these are some basic imports for to necessary in the below operations. Here we can perform the Selenium Keys command called keyUp(keys) and keyDown(keys) in the request inputs. Also we triggered and perform the operations using the Actions class and after creation of Actions() class and we pass the parameters like Driver instance then we can trigger the actions. First we sue the method like moveToElement() and passed WebElement then the mouse or keyboard uses so the actions called with the help of keyDown() passing the parameter like keys.CONTROL then we click() the specific link again we used keyUp() for to release the actions like keys.CONTROL. Finally we used build().perform() operation for closing the events and operations then only these action will be performed else it won’t works.
Example #2
Code:
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class New {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Kripya-PC\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe");
WebDriver dr = new ChromeDriver();
dr.manage().window().maximize();
dr.get("https://www.facebook.com/");
dr.findElement(By.xpath("//button[@value='1']")).sendKeys(Keys.ENTER);
}
}
Output:
In the above example we used Keys.ENTER operations for to perform the selenium operations from the request URL inputs. Here we cannot use any Actions classes and its methods instead of that we used simply sendKeys() method from the webdrivers. After finding the web element from the web page then we need to perform the operations automatically like Login and Submit buttons. Because without email or mobile number and passwords the facebook is not able to login do that it will throws error.
Conclusion
We can use different Keys in the selenium automation like ENTER or Keys. For each keys have different set of features and actions triggered from the back end. Like that when we press the Enter key over a textbox it will RETURN to the textbox’s sendKeys function or some validation error is thrown from the application.
Recommended Articles
We hope that this EDUCBA information on “Selenium Keys” was beneficial to you. You can view EDUCBA’s recommended articles for more information.