webdriver-鍵盤操作 for java
阿新 • • 發佈:2018-07-31
空格 firefox new oar eve ndk [] ive .get
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();
}
}
}
2017年01月17日 17:08:25 閱讀數:2044 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();
}
}
}
webdriver-鍵盤操作 for java