简体   繁体   中英

Send caps text to a text box using Robot class & Selenium WebDriver

如何使用Robot类和Selenium WebDriver将大写文本发送到文本框。

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);

You need to do two things to:

1- First to get focus to the textfield, where you want to enter the value, like this:

driver.findElement(By.xpath("//xpath of the element")).sendKeys("")// id or class can be used as locators too.

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" :

//Navigating to the site
driver.get("http://www.google.com");

//To get the focus on the searchbox (NOT ENTERING ANYTHING)
driver.findElement(By.id("gbqfq")).sendKeys("");

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CAPS_LOCK);
robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
robot.keyPress(KeyEvent.VK_H);
robot.keyRelease(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_E);
robot.keyRelease(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_L);
robot.keyRelease(KeyEvent.VK_L);
robot.keyPress(KeyEvent.VK_L);
robot.keyRelease(KeyEvent.VK_L);
robot.keyPress(KeyEvent.VK_O);
robot.keyRelease(KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_CAPS_LOCK);
robot.keyRelease(KeyEvent.VK_CAPS_LOCK);

OR you can try using "SHIFT" as below:

    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_SHIFT);
    robot.keyPress(KeyEvent.VK_H);
    robot.keyRelease(KeyEvent.VK_H);
    robot.keyPress(KeyEvent.VK_E);
    robot.keyRelease(KeyEvent.VK_E);
    robot.keyPress(KeyEvent.VK_L);
    robot.keyRelease(KeyEvent.VK_L);
    robot.keyPress(KeyEvent.VK_L);
    robot.keyRelease(KeyEvent.VK_L);
    robot.keyPress(KeyEvent.VK_O);
    robot.keyRelease(KeyEvent.VK_O);
    robot.keyRelease(KeyEvent.VK_SHIFT);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM