詳解Java Selenium中的鼠標(biāo)控制操作
簡介
本文主要講解如何用java Selenium 控制鼠標(biāo)在瀏覽器上的操作方法。主要列舉的代碼示例,無圖顯示??梢宰约荷洗a執(zhí)行操作看效果。
鼠標(biāo)控制
單擊左鍵
模擬完成單擊鼠標(biāo)左鍵的操作,一般點(diǎn)擊進(jìn)入子頁面等會(huì)用到。
第一種通過WebElement對象的click()方法實(shí)現(xiàn)單擊左鍵
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumDemo {
private final static String webDriver = "webdriver.chrome.driver";
private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";
public static void main(String[] args) throws InterruptedException {
System.setProperty(webDriver, webDriverPath);
WebDriver driver= new ChromeDriver();
//Jenkins 登錄界面
driver.get("http://119.167.159.214:8080/login");
Thread.sleep(2000);
//定位按鈕元素
WebElement commentPlugin=driver.findElement(By.name("Submit"));
//執(zhí)行單擊操作
commentPlugin.click();
}
}
第二種通過Actions對象的click()方法實(shí)現(xiàn)單擊左鍵
//定位按鈕元素
WebElement commentPlugin=driver.findElement(By.name("Submit"));
// 實(shí)例化Actions類對象:actions,并將driver傳給actions
Actions actions = new Actions(driver);
//無定位點(diǎn)擊
actions.click().perform();
//定位web元素后點(diǎn)擊
actions.click(commentPlugin).perform();
.perform()方法是動(dòng)作執(zhí)行的意思,每個(gè)動(dòng)作方法必須再使用.perform()才能執(zhí)行。
單擊右鍵
鼠標(biāo)右擊的操作與左擊有很大不同,需要使用 Actions 。
//定位按鈕元素
WebElement commentPlugin=driver.findElement(By.name("Submit"));
// 實(shí)例化Actions類對象:actions,并將driver傳給actions
Actions actions = new Actions(driver);
//無定位右鍵點(diǎn)擊
actions.contextClick().perform();
//定位web元素后右鍵點(diǎn)擊
actions.contextClick(commentPlugin).perform();
雙擊左鍵
模擬鼠標(biāo)雙擊操作。
//定位按鈕元素
WebElement commentPlugin=driver.findElement(By.name("Submit"));
// 實(shí)例化Actions類對象:actions,并將driver傳給actions
Actions actions = new Actions(driver);
//無定位雙擊
actions.doubleClick().perform();
//定位web元素后雙擊
actions.contextClick(commentPlugin).perform();
按壓左鍵
模擬鼠標(biāo)按下左鍵不松手
//無定位鼠標(biāo)按壓左鍵
actions.clickAndHold().perform();
//定位web元素后鼠標(biāo)按壓左鍵
actions.clickAndHold(commentPlugin).perform();
鼠標(biāo)箭頭移動(dòng)
模擬鼠標(biāo)箭頭移動(dòng)
//移動(dòng)到定位的元素位置上 actions.moveToElement(commentPlugin).perform();
鼠標(biāo)釋放
模擬鼠標(biāo)按壓后,釋放鼠標(biāo)
//鼠標(biāo)釋放
actions.release().perform();
//定位釋放鼠標(biāo)釋放
actions.release(commentPlugin).perform();
鼠標(biāo)拖拽
模擬鼠標(biāo)選中web元素后拖拽到指定位置的操作
//需要拖拽的web元素
WebElement source=driver.findElement(By.id("source"));
//拖拽的目標(biāo)元素的位置
WebElement target=driver.findElement(By.id("target"));
//拖拽操作
actions.dragAndDrop(source,target).perform();
鼠標(biāo)等待
一般點(diǎn)擊網(wǎng)頁的某個(gè)按鈕,網(wǎng)頁需要渲染一端時(shí)間才會(huì)出現(xiàn)新的dom樹,所以我們需要操作等待執(zhí)行。
//定位web元素后鼠標(biāo)按壓左鍵
actions.clickAndHold(commentPlugin).perform();
//動(dòng)作等待3秒
actions.pause(5000);
//釋放鼠標(biāo)左鍵
actions.release(commentPlugin).perform();到此這篇關(guān)于詳解Java Selenium中的鼠標(biāo)控制操作的文章就介紹到這了,更多相關(guān)Java Selenium鼠標(biāo)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
spring*.xml配置文件明文加密的實(shí)現(xiàn)
SpringMVC RESTful支持實(shí)現(xiàn)過程演示
一文搞懂Spring中@Autowired和@Resource的區(qū)別
因不會(huì)遠(yuǎn)程debug調(diào)試我被項(xiàng)目經(jīng)理嘲笑了
基于Java方式實(shí)現(xiàn)數(shù)據(jù)同步

