十三章 Selenium Webdriver-按键
1.选中元素输入键盘中的值
package Keypress;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class KeypressDemo {
WebDriver driver = new ChromeDriver();
String url = "https://www.baidu.com/";
@Before
public void setUp() throws Exception {
driver.manage().window().maximize();
Thread.sleep(8);
}
@Test
public void name() throws Exception {
driver.get(url);
driver.findElement(By.id("kw")).sendKeys("测试"+ Keys.ENTER);
Thread.sleep(3000);
driver.findElement(By.id("kw")).sendKeys(Keys.TAB);
driver.findElement(By.id("kw")).sendKeys(Keys.TAB);
driver.findElement(By.id("kw")).sendKeys(Keys.TAB);
Thread.sleep(3000);
Thread.sleep(3000);
driver.findElement(By.id("kw")).sendKeys(Keys.chord(Keys.CANCEL,"a"));
}
@After
public void tearDown() throws Exception {
}
}
2.用action类处理按键事件
package Keypress;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class KeypressDemo1 {
WebDriver driver = new ChromeDriver();
String url = "https://www.baidu.com/";
@Before
public void setUp() throws Exception {
driver.manage().window().maximize();
Thread.sleep(8);
}
@Test
public void name() throws Exception {
driver.get(url);
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();
}
@After
public void tearDown() throws Exception {
}
}