How to send capital text to a text box using Robot class and Selenium WebDriver.
Send caps text to a text box using Robot class & Selenium WebDriver
14.7k views Asked by dev At
3
There are 3 answers
8
On
You can send caps text to a text box using Robot class in following manner. Below i am sending String OK using Robot class
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CAPS_LOCK);
robot.keyPress(KeyEvent.VK_O);
robot.keyRelease(KeyEvent.VK_O);
robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
robot.keyPress(KeyEvent.VK_CAPS_LOCK);
robot.keyPress(KeyEvent.VK_K);
robot.keyRelease(KeyEvent.VK_K);
robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
0
On
It can be done by using "Caps Lock" or "Shift" key, that code has been mentioned here in another answer by Subh.
You can also do it by using StringSelection in Java. The code is as below:
//First of all declare the method setClipboardData as below:
public void setClipboardData(String string) {
StringSelection stringSelection = new StringSelection(string);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
//Call the method setClipboardData and write other Robot code:
driver.get("https://www.google.com/");
driver.findElement(By.id("lst-ib")).clear();
driver.findElement(By.id("lst-ib")).sendKeys("");
Robot robot = new Robot();
setClipboardData("ALL CAPS");
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
You need to do two things to:
1- First to get focus to the textfield, where you want to enter the value, like this:
2- Then use 'Robot class' to send values to the field (using CAPSLOCK or SHIFT keys for changing the letters to uppercase).
Try this code. It works for sending "HELLO" (all caps) in Google.com's search field using "CAPSLOCK":
OR you can try using "SHIFT" as below: