有没有办法在文本字段中输入数据而不给予webelement

时间:2018-03-01 15:14:44

标签: selenium-webdriver automation

我需要在突出显示的选项卡上发送数据,但不希望通过findElement传递数据。我的代码如下所示。请指教。

 public class A003_KeyBoardActions {
 public static void main(String[] args) throws Exception{
 System.setProperty("WebDriver.driver","C:eclipse\\IEDriverServer.exe");
 WebDriver driver=new ChromeDriver();
 driver.manage().window().maximize();
 driver.get("URL");
 Robot r=new Robot();
 r.keyPress(java.awt.event.KeyEvent.VK_TAB);
 r.keyRelease(java.awt.event.KeyEvent.VK_TAB);
 System.out.println("Cursor moved to home page");
 r.keyPress(java.awt.event.KeyEvent.VK_TAB);
 r.keyRelease(java.awt.event.KeyEvent.VK_TAB);
 System.out.println("Courser moved to username");
 **HERE I NEED TO SEND DATA ON HIGHLIGHTED TAB** I dont want to pass data by 
 findElement.
 Thread.sleep(1000);
 driver.close();
 }
 }

1 个答案:

答案 0 :(得分:0)

你可以这样做。

String userName = "YourGoodName";
StringSelection stringSelection = new StringSelection(userName);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);