您好,登錄后才能下訂單哦!
在WebDriver中鼠標(biāo)操作的方法封裝在ActionChains類中
ActionChains類提供的常用方法:
perform():執(zhí)行所有ActionChains中的存儲行為
contextclick() 右擊
double_click() 雙擊
drag_and_drop() 拖動
move_to_element() 鼠標(biāo)懸停
鼠標(biāo)右擊事件
下面代碼中:from selenium.driver import ActionChains 導(dǎo)入提供鼠標(biāo)操作的ActionChains類
ActionChains(driver)調(diào)用ActionChains類,將瀏覽器驅(qū)動driver作為參數(shù)傳入
context_click(right_click)方法用于模擬鼠標(biāo)右鍵操作,在調(diào)用時需要指定元素定位
perform()執(zhí)行所有ActionChains中的存儲行為,對整個操作的提交動作。
2.鼠標(biāo)懸停 move_to_element()方法可以模擬鼠標(biāo)懸停的動作 3.鼠標(biāo)雙擊操作 使用double_click函數(shù) 4.鼠標(biāo)拖放操作 drag_and_drop(source,target)在源元素上按住鼠標(biāo)左鍵,然互移動到目標(biāo)機(jī)上釋放 source:鼠標(biāo)拖動的源元素 target鼠標(biāo)釋放的目標(biāo)元素 from selenium import webdriver from time import * from selenium.webdriver.common.action_chains import ActionChains driver = webdriver.Chrome() url = "https://www.baidu.com" print('new access %s' %(url)) driver.get(url) #定位元素 right_click= driver.find_element_by_xpath('//*[@id="u1"]/a[8]') #鼠標(biāo)右擊 ActionChains(driver).context_click(right_click).perform() #鼠標(biāo)懸停 #ActionChains(driver).move_to_element(right_click).perform() #鼠標(biāo)雙擊 ActionChains(driver).double_click(right_click).perform() sleep(3) driver.quit()
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。