常用的操作事件(右鍵點(diǎn)擊,頁(yè)面滑動(dòng),表單操作等)
Actions
Actions:執(zhí)行 PC 端的鼠標(biāo)點(diǎn)擊、雙擊、右鍵、拖拽等事件
TouchActions:模擬 PC 和移動(dòng)端的點(diǎn)擊、滑動(dòng)、拖拽、多點(diǎn)觸控等多種手勢(shì)操作。
Acitons 方法列表
Actions 類是 Selenium 對(duì)鼠標(biāo)、鍵盤操作的。
常用的操作包括:
click——單擊鼠標(biāo)左鍵
click_and_hold——點(diǎn)擊鼠標(biāo)左鍵,不松開(kāi)
context_click——點(diǎn)擊鼠標(biāo)右鍵
double_click——雙擊鼠標(biāo)左鍵
drag_and_drop——拖拽到某個(gè)元素到目標(biāo)位置后松開(kāi)
drag_and_drop_by_offset——拖拽到某個(gè)坐標(biāo)然后松開(kāi)
move_by_offset——鼠標(biāo)從當(dāng)前位置移動(dòng)到某個(gè)坐標(biāo)
move_to_element——鼠標(biāo)移動(dòng)到某個(gè)元素
move_to_element_with_offset——移動(dòng)到距某個(gè)元素(左上角坐標(biāo))多少距離的位置
perform()——執(zhí)行鏈中的所有動(dòng)作
releas——在某個(gè)元素位置松開(kāi)鼠標(biāo)左鍵
send_keys——發(fā)送某個(gè)鍵到當(dāng)前焦點(diǎn)的元素
key_down——按下某個(gè)鍵盤上的鍵
key_up——松開(kāi)某個(gè)鍵
動(dòng)作鏈接Actions
- 執(zhí)行原理:
- 調(diào)用 Acitons 的方法時(shí),不會(huì)立即執(zhí)行,而是將所有的操作,按順序存放在一個(gè)隊(duì)列里,當(dāng)你調(diào)用 perform() 方法時(shí),隊(duì)列中的事件會(huì)依次執(zhí)行
- 基本用法
- 生成一個(gè)動(dòng)作 Actions actions = new Actions(driver)
- 動(dòng)作添加方法1 actions.方法1
- 動(dòng)作添加方法2 actions.方法2
- 調(diào)用 perform() 方法執(zhí)行 acitons.perform()
- 具體寫法
- 鏈?zhǔn)綄懛?
Actions actions = new Actions(driver).moveToElement(ele).click().perform(ele); - 分步寫法
Actions actions = new Actions(driver); actions.move_to_element(element); actions.click(element); actions.perform();
- 鏈?zhǔn)綄懛?
Actions 用法1
- 用法一:點(diǎn)擊,右鍵,雙擊操作
Actions actions = new Actions(driver); actions.click(element); actions.double_click(element); actions.context_click(element); actions.perform();
Actions 用法2
- 用法二:鼠標(biāo)移動(dòng)到某個(gè)元素上
Actions actions = new Actions(driver); actions.move_to_element(element); actions.perform();
Actions 用法3
- 用法三:將一個(gè)元素拖拽到另一個(gè)元素的位置上
Actions actions = new Actions(driver); actions.drag_and_hold(element_start,element_end).perform() 或者 actions.click_and_hold(element_start).move_to_element(element_end).release().perform();
Actions 用法4
- 用法四:Actions 模擬按鍵方法
- 模擬按鍵有多種方法,能用 win32api 來(lái)實(shí)現(xiàn),能用 SendKeys 來(lái)實(shí)現(xiàn),也可用 selenium 的 WebElement 對(duì)象的 send_keys() 方法來(lái)實(shí)現(xiàn),這里 Actions 類也提供了幾個(gè)模擬按鍵的方法。
- 用法:
Actions actions = Actions(driver); actions.send_keys(Keys.BACK_SPACE); 或者 actions.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL); actions.perform();