webdriver提供Actions类,来模拟鼠标点击、悬浮、拖拽、键盘输入等操作;
一、鼠标双击、右击
selenium模拟鼠标单击是用WebElement.click(); 方法,但是双击、右击,需要使用Actions类来模拟;
package com.automation.actions; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; /** * 类说明:模拟鼠标双击、右击的操作 * <br/> * @version 1.0 * 2016年11月21日 下午8:41:14 */ public class DoubleClickAndRightClick { private static WebDriver driver = null ; private static String chromeDriverDir = "D:\\workspace\\A_Test\\resource\\chromedriver.exe"; public static void main(String[] args) { //1.打开浏览器; System.setProperty("webdriver.chrome.driver", chromeDriverDir); driver = new ChromeDriver(); driver.manage().window().maximize(); //打开文件网址; driver.get("http://www.baidu.com/"); //定位百度输入框对象; WebElement inputBox = driver.findElement(By.id("kw")); /* * 双击、右击输入框元素, * 1.需要声明Actions类对象; * 2.使用Actions对象的doubleClick、contextClick方法实现双击、右击操作; */ //声明Actions类对象; Actions action = new Actions(driver); //双击操作; action.doubleClick(inputBox).build().perform(); //右击操作; action.contextClick(inputBox).build().perform(); driver.quit(); } }
二、鼠标悬浮在指定元素上方
在有些页面上,需要鼠标停留在指定的元素上方,才能触发一些页面响应,例如隐藏元素、菜单等,这就需要模拟鼠标悬浮在元素之上;
package com.automation.actions; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; /** * 类说明:模拟鼠标悬浮在元素之上的操作 * <br/> * @version 1.0 * 2016年11月21日 下午8:52:50 */ public class MoveToElement { private static WebDriver driver = null ; private static String chromeDriverDir = "D:\\workspace\\A_Test\\resource\\chromedriver.exe"; public static void main(String[] args) { //1.打开浏览器; System.setProperty("webdriver.chrome.driver", chromeDriverDir); driver = new ChromeDriver(); driver.manage().window().maximize(); //打开文件网址; driver.get("http://www.baidu.com/"); //定位百度搜索按钮对象; WebElement searchButton = driver.findElement(By.id("su")); /* * 模拟鼠标停留在搜索按钮上方, * 1.需要声明Actions类对象; * 2.使用Actions对象的moveToElement方法实现鼠标悬浮操作; */ //声明Actions类对象; Actions action = new Actions(driver); //模拟鼠标悬浮在按钮上 action.moveToElement(searchButton).perform(); driver.quit(); } }
三、模拟鼠标托转
可以模拟有些网页的元素,能够被移动到其他位置;
package com.automation.actions; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; /** * 类说明:模拟鼠标悬浮在元素之上的操作 * <br/> * @version 1.0 * 2016年11月21日 下午9:00:50 */ public class DragAndDrop { private static WebDriver driver = null ; private static String chromeDriverDir = "D:\\workspace\\A_Test\\resource\\chromedriver.exe"; public static void main(String[] args) { //1.打开浏览器; System.setProperty("webdriver.chrome.driver", chromeDriverDir); driver = new ChromeDriver(); driver.manage().window().maximize(); //打开文件网址; driver.get("http://www.baidu.com/"); //定位百度搜索按钮对象; WebElement draggable = driver.findElement(By.id("su")); /* * 模拟鼠标拖拽元素对象, * 1.需要声明Actions类对象; * 2.使用Actions对象的dragAndDropBy方法实现鼠标拖拽操作; */ //声明Actions类对象; Actions action = new Actions(driver); //向右拖动10个像素,共拖拽5次 for (int i = 0; i < 5; i++) { //10表示元素的横坐标向右移动10个像素,0表示元素的纵坐标不变; action.dragAndDropBy(draggable, 10, 0).build().perform(); } driver.quit(); } }
四、模拟键盘操作
package com.automation.actions; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; /** * 类说明:模拟键盘的操作 * <br/> * @version 1.0 * 2016年11月21日 下午9:00:50 */ public class KeyBoard { private static WebDriver driver = null ; private static String chromeDriverDir = "D:\\workspace\\A_Test\\resource\\chromedriver.exe"; public static void main(String[] args) { //1.打开浏览器; System.setProperty("webdriver.chrome.driver", chromeDriverDir); driver = new ChromeDriver(); driver.manage().window().maximize(); //打开文件网址; driver.get("http://www.baidu.com/"); //声明Actions类对象; Actions action = new Actions(driver); action.keyDown(Keys.CONTROL);//点击control键 action.keyDown(Keys.SHIFT);//点击shift键 action.keyUp(Keys.CONTROL);//松开control键 action.keyUp(Keys.SHIFT);//松开shift键 action.keyDown(Keys.TAB);//点击Tab键 action.keyUp(Keys.TAB);//松开tab键 driver.quit(); } }
尤其在搜索过程中,如果有联想功能,就需要用到键盘的上、下键来操作,这就需要使用到模拟键盘;