1 package com.test.key; 2 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.Keys; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.firefox.FirefoxDriver; 7 8 public class KeyB { 9 10 public static void main(String[] args) { 11 WebDriver driver = new FirefoxDriver(); 12 driver.get("http://www.baidu.com"); 13 driver.manage().window().maximize(); 14 15 //输入框输入内容 16 driver.findElement(By.xpath("//*[@id='kw']")).sendKeys("selenium"); 17 18 //删除一个m 19 driver.findElement(By.xpath("//*[@id='kw']")).sendKeys(Keys.BACK_SPACE); 20 21 //输入m 空格 教程 22 driver.findElement(By.xpath("//*[@id='kw']")).sendKeys("m"); 23 driver.findElement(By.xpath("//*[@id='kw']")).sendKeys(Keys.SPACE); 24 driver.findElement(By.xpath("//*[@id='kw']")).sendKeys("教程"); 25 26 //全选输入框内容 27 driver.findElement(By.xpath("//*[@id='kw']")).sendKeys(Keys.CONTROL,"a"); 28 29 //剪切输入框内容 30 driver.findElement(By.xpath("//*[@id='kw']")).sendKeys(Keys.CONTROL,"x"); 31 32 //粘贴输入框内容 33 driver.findElement(By.xpath("//*[@id='kw']")).sendKeys(Keys.CONTROL,"v"); 34 35 //通过回车来代替提交动作 36 driver.findElement(By.xpath("//*[@id='kw']")).sendKeys(Keys.ENTER); 37 38 waitTime(5000); 39 driver.quit(); 40 } 41 42 static public void waitTime(int time) { 43 44 try { 45 Thread.sleep(time); 46 } catch (InterruptedException e) { 47 // TODO Auto-generated catch block 48 e.printStackTrace(); 49 } 50 } 51 52 }