技術(shù)分享 | web 控件的交互進(jìn)階
本文節(jié)選自霍格沃茲測試開發(fā)學(xué)社內(nèi)部教材
當(dāng)需要模擬鍵盤或者鼠標(biāo)操作時,Python需要使用 ActionChains 來處理,Java需要 Actions 來處理。
常用模擬鼠標(biāo)的行為,比如單擊,雙擊,拖動等。當(dāng)調(diào)用 ActionChains 或者 Actions 的方法時,會將所有操作按順序存入隊列,當(dāng)調(diào)用 perform() 方法時,隊列中的事件會依次執(zhí)行。
Python 版本
# 引入依賴 from selenium.webdriver import ActionChains
Java版本
import org.openqa.selenium.interactions.Actions;
實戰(zhàn)演示
下面代碼中,action是模擬鍵盤或者鼠標(biāo)的實例對象,on_element 是需要傳遞一個元素進(jìn)去,默認(rèn)值為 None。
單擊指定元素,如果不指定,會單擊當(dāng)前光標(biāo)的位置
Python 版本
action.click(on_element=None)
Java版本
Actions action = new Actions(webDriver); action.click(on_element=None);
長按某個元素
Python 版本
action.click_and_hold(on_element=None)
Java版本
Actions action = new Actions(webDriver); action.clickAndHold(on_element=None);
執(zhí)行右鍵操作
Python 版本
action.context_click(on_element=None)
Java版本
Actions action = new Actions(webDriver); action.contextClick(on_element=None);
執(zhí)行左鍵雙擊
Python 版本
action.double_click(on_element=None)
Java版本
Actions action = new Actions(webDriver); action.doubleClick(on_element=None);
拖拽起始的元素到目標(biāo)元素,即 source 到 target
Python 版本
action.drag_and_drop(source, target)
Java版本
Actions action = new Actions(webDriver); action.dragAndDrop(WebElement source, WebElement target);
將目標(biāo)拖動到指定的位置
Python 版本
# xoffset 和 yoffset 是相對于 source 左上角為原點的偏移量 action.drag_and_drop_by_offset(source, xoffset, yoffset)
Java版本
Actions action = new Actions(webDriver); actions.dragAndDropBy(WebElement source, int xOffset, int yOffset);
使用這個方法可以方便的實現(xiàn)某些組合鍵盤事件,比如按下 ctrl+c 鍵。
Python 版本
action.key_down(value, element=None)
Java版本
Actions action = new Actions(webDriver); actions.keyDown(element, value);
松開某個鍵,可以配合上面的方法實現(xiàn)按下 ctrl+c 并且釋放
Python 版本
ActionChains(driver).key_down(Keys.CONTROL)\ .send_keys('c').key_up(Keys.CONTROL).perform()
Java版本
Actions action = new Actions(webDriver); action.keyDown(Keys.CONTROL).sendKeys("c").keyUp(Keys.CONTROL).perform();
其他按鍵請參考:https://python-selenium-zh.readthedocs.io/zh_CN/latest/7.4 特殊字符/
github 參考地址:https://github.com/SeleniumHQ/selenium/blob/916168f403dded05f878fe189d68c0f9152335c9/py/selenium/webdriver/common/keys.py
指定光標(biāo)移動到某一個位置,需要給出兩個坐標(biāo)位置
Python 版本
# xoffset 和 yoffset 是相對于網(wǎng)頁左上角的偏移量 action.move_by_offset(xoffset, yoffset)
Java版本
Actions action = new Actions(webDriver); action.moveByOffset(xOffset,yOffset);
將鼠標(biāo)移動到指定元素的位置
Python 版本
action.move_to_element(to_element)
Java版本
Actions action = new Actions(webDriver); action.moveToElement(to_element);
移動鼠標(biāo)到相對于某個元素的偏移位置
Python 版本
# xoffset 和 yoffset 是相對于 to_element 左上角的偏移量 action.move_to_element_with_offset(to_element, xoffset, yoffset)
Java版本
Actions action = new Actions(webDriver); action.moveToElement(to_element, xOffset, yOffset);
執(zhí)行 ActionChains 中的操作
前面介紹的方法會將所有操作按順序存入隊列,要執(zhí)行這些操作,需要調(diào)用 perform() 方法。
Python 版本
action.move_to_element_with_offset(to_element, xoffset, yoffset).perform()
Java版本
Actions action = new Actions(webDriver); action.moveToElement(to_element, int xOffset, int yOffset).perform();
釋放按下的鼠標(biāo)
Python 版本
action.release(on_element=None)
Java版本
Actions action = new Actions(webDriver); action.release(on_element=None)
向焦點元素位置輸入值
焦點元素:使用 tab 鍵,那些被選中的元素就是焦點元素。
Python 版本
action.send_keys(*keys_to_send)
Java版本
Actions action = new Actions(webDriver); action.sendKeys(*keys_to_send)
向指定的元素輸入數(shù)據(jù)
Python 版本
action.send_keys_to_element(element, *keys_to_send)
Java版本
Actions action = new Actions(webDriver); action.sendKeys(element,keys_to_send);
Python web前端
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。