如何使用Robot类和Selenium WebDriver将大写文本发送到文本框。
答案 0 :(得分:0)
您可以使用Robot类以下列方式将大写文本发送到文本框。 下面我使用Robot类发送String OK
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);
答案 1 :(得分:0)
你需要做两件事:
1-首先将焦点转移到要输入值的文本字段,如下所示:
driver.findElement(By.xpath("//xpath of the element")).sendKeys("")// id or class can be used as locators too.
2-然后使用'Robot class'向字段发送值(使用CAPSLOCK或SHIFT键将字母更改为大写)。
试试这段代码。它适用于使用“CAPSLOCK” 在Google.com的搜索字段中发送“HELLO”(全部上限):
//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);
或您可以尝试使用“SHIFT”,如下所示:
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);
答案 2 :(得分:0)
可以使用“Caps Lock”或“Shift”键完成,该代码已在Subh的另一个答案中提及。
您也可以在Java中使用StringSelection来完成。代码如下:
//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);