注:driver爲一個WebDriver的實例,xpath爲一個元素的xpath字符串,在本文中一律採用xpath的方式定位元素
1、鼠標右鍵點擊操作:Actions action = new Actions(driver) ;
action.contextClick(driver.findElement(By.xpath(xpath))) ;
2、鼠標左鍵雙擊操作:
Actions action = new Actions(driver) ;
action.doubleClick(driver.findElement(By.xpath(xpath))) ;
3、鼠標左鍵按下操作:
Actions action = new Actions(driver) ;
action.clickAndHold(driver.findElement(By.xpath(xpath))) ;
4、鼠標左鍵擡起操作:
Actions action = new Actions(driver) ;
action.release(driver.findElement(By.xpath(xpath))) ;
5、鼠標移動到元素上操作:
Actions action = new Actions(driver) ;
action.moveToElement(driver.findElement(By.xpath(xpath))) ;
6、組合的鼠標操作(將目標元素拖拽到指定的元素上):
Actions action = new Actions(driver) ;
action.dragAndDrop(driver.findElement(By.xpath(xpath)),driver.findElement(By.xpath(xpath))) ;
7、組合的鼠標操作(將目標元素拖拽到指定的區域裏):
Actions action = new Actions(driver) ;
action.dragAndDrop(driver.findElement(By.xpath(xpath)),xOffset,yOffset) ;
8、鍵盤的按下操作:
Actions action = new Actions(driver) ;
action.keyDown(driver.findElement(getBy()),key) ;注:key 爲一個Keys的實例,實例化一個F1的按鍵則爲Keys.F1
9、按鈕鬆開操作:
Actions action = new Actions(driver) ;
action.keyUp(driver.findElement(getBy()),key) ;