import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class selenium_keyboard_action {
/*Keys类提供了键盘的相关方法,sendKeys模拟键盘输入,可以输入键盘上的按键,也可以模拟组合键*/
WebDriver driver=new FirefoxDriver();
Actions action=new Actions(driver);
public static void main(String[] args) throws Exception {
selenium_keyboard_action ska=new selenium_keyboard_action();
ska.init();
//ska.test1();
//ska.test2();
ska.robotUse();
}
public void init(){
driver.get("http://www.baidu.com/");
}
public void test1(){
WebElement element=driver.findElement(By.id("kw"));
element.sendKeys("test");//A向百度搜索框中输入test
element.sendKeys(Keys.BACK_SPACE);//删除最后一个t
element.sendKeys(Keys.SPACE);//空格键
element.sendKeys("ting");//输入ting
element.sendKeys(Keys.CONTROL,"a");//ctrl+a
element.sendKeys(Keys.CONTROL,"x");//ctrl+x
element.sendKeys(Keys.CONTROL,"v");//ctrlv
element.sendKeys(Keys.ENTER);//ctrl+a
}
public void test2() throws Exception{//对右键菜单使用快捷键
action.contextClick(driver.findElement(By.id("su"))).perform();
Thread.sleep(2000);
//action.sendKeys("v").perform();//模拟键盘输入v
action.sendKeys(Keys.ARROW_DOWN).perform();//通过键盘按3次向下的箭头
action.sendKeys(Keys.ARROW_DOWN).perform();
action.sendKeys(Keys.ARROW_DOWN).perform();
action.sendKeys(Keys.ENTER).perform();//按回车键
}
public void robotUse() throws Exception{//使用robot模拟鼠标键盘的操作
action.moveToElement(driver.findElement(By.linkText("登录")));
try {
Thread.sleep(2000);
Robot r=new Robot();
//r.mousePress(InputEvent.BUTTON1_MASK);//按下鼠标左键
//r.mouseRelease(InputEvent.BUTTON1_MASK);
r.keyPress(KeyEvent.VK_DOWN);//在下拉菜单中点击一次键盘的向下键
r.keyRelease(KeyEvent.VK_DOWN);
r.keyPress(KeyEvent.VK_ENTER);//按下回车键
r.keyRelease(KeyEvent.VK_ENTER);//释放回车键
} catch (AWTException e) {
e.printStackTrace();
}
}
}
import java.awt.Robot;
import java.awt.event.InputEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class selenium_keyboard_action {
/*Keys类提供了键盘的相关方法,sendKeys模拟键盘输入,可以输入键盘上的按键,也可以模拟组合键*/
WebDriver driver=new FirefoxDriver();
Actions action=new Actions(driver);
public static void main(String[] args) throws Exception {
selenium_keyboard_action ska=new selenium_keyboard_action();
ska.init();
//ska.test1();
//ska.test2();
ska.robotUse();
}
public void init(){
driver.get("http://www.baidu.com/");
}
public void test1(){
WebElement element=driver.findElement(By.id("kw"));
element.sendKeys("test");//A向百度搜索框中输入test
element.sendKeys(Keys.BACK_SPACE);//删除最后一个t
element.sendKeys(Keys.SPACE);//空格键
element.sendKeys("ting");//输入ting
element.sendKeys(Keys.CONTROL,"a");//ctrl+a
element.sendKeys(Keys.CONTROL,"x");//ctrl+x
element.sendKeys(Keys.CONTROL,"v");//ctrlv
element.sendKeys(Keys.ENTER);//ctrl+a
}
public void test2() throws Exception{//对右键菜单使用快捷键
action.contextClick(driver.findElement(By.id("su"))).perform();
Thread.sleep(2000);
//action.sendKeys("v").perform();//模拟键盘输入v
action.sendKeys(Keys.ARROW_DOWN).perform();//通过键盘按3次向下的箭头
action.sendKeys(Keys.ARROW_DOWN).perform();
action.sendKeys(Keys.ARROW_DOWN).perform();
action.sendKeys(Keys.ENTER).perform();//按回车键
}
public void robotUse() throws Exception{//使用robot模拟鼠标键盘的操作
action.moveToElement(driver.findElement(By.linkText("登录")));
try {
Thread.sleep(2000);
Robot r=new Robot();
//r.mousePress(InputEvent.BUTTON1_MASK);//按下鼠标左键
//r.mouseRelease(InputEvent.BUTTON1_MASK);
r.keyPress(KeyEvent.VK_DOWN);//在下拉菜单中点击一次键盘的向下键
r.keyRelease(KeyEvent.VK_DOWN);
r.keyPress(KeyEvent.VK_ENTER);//按下回车键
r.keyRelease(KeyEvent.VK_ENTER);//释放回车键
} catch (AWTException e) {
e.printStackTrace();
}
}
}