一、键盘模拟常用的键
- sendKeys(Keys.BACK_SPACE); //删除键--Backspace
- sendKeys(Keys.SPACE); //空格键 Space
- sendKeys(Keys.TAB); //制表键 Tab
- sendKeys(Keys.ESCAPE); //回退键 ESC
- sendKeys(Keys.ENTER); //回车键 Enter
- sendKeys(Keys.F12); //键盘F5(可模拟F1-F12)
- sendKeys(Keys.CONTROL,"a"); //全选 Ctrl+A
- sendKeys(Keys.CONTROL,"c"); //复制 Ctrl+C
- sendKeys(Keys.CONTROL,"x"); //剪切 Ctrl+X
- sendKeys(Keys.CONTROL,"v"); //粘贴 Ctrl+V
二、鼠标模拟
需要使用 org.openqa.selenium.interactions.Actions; 类 来模拟鼠标操作,常用的方法有:
Actions actions =new Actions(driver);
- actions.clickAndHold(driver.findElement(By.linkText("家用电器"))).perform(); //鼠标悬浮
- actions.doubleClick(driver.findElement(By.id(""))).perform(); //鼠标双击
- actions.contextClick(driver.findElement(By.id(""))).perform(); //鼠标右击
- actions.dragAndDrop(source, target); //拖拽操作
有时候在切换框架定位元素时可能出现无焦点的问题,则这个时候就需要使用鼠标点击操作,来完成聚焦
WebDriver driver=null;
Actions actions =new Actions(driver);
WebElement element= driver.findElement(By.id("su"));
actions.moveToElement(element).click().sendKeys("dddd").perform();