Python selenium鍵盤鼠標(biāo)事件實現(xiàn)過程詳解
引言
----在實際的web測試工作中,需要配合鍵盤按鍵來操作,webdriver的 keys()類提供鍵盤上所有按鍵的操作,還可以模擬組合鍵Ctrl+a,Ctrl+v等。
舉例:
#cording=gbkimport osimport timefrom selenium import webdriverfrom selenium.webdriver.common.by import By #導(dǎo)入by方法from selenium.webdriver.common.action_chains import ActionChains ##對鼠標(biāo)事件操作from selenium.webdriver.common.keys import Keys # 對鍵盤事件操作current_path=os.path.dirname(__file__)firefox_path=current_path+'/../webdriver/geckodriver.exe'driver=webdriver.Firefox(executable_path=firefox_path)driver.get('http://www.baidu.com')# 先輸入百度driver.find_element_by_id(’kw’).send_keys(’百度’)time.sleep(3)# 1.刪除度driver.find_element_by_id(’kw’).send_keys(Keys.BACK_SPACE)time.sleep(3)#2.清空輸入框,重新輸入值driver.find_element_by_id(’kw’).clear()driver.find_element_by_id(’kw’).send_keys(’安琪兒’)time.sleep(5)# 3.ctrl+a 全選輸入框里的內(nèi)容driver.find_element_by_id(’kw’).send_keys(Keys.CONTROL, ’a’)time.sleep(3)# 4.ctrl+x 剪切輸入框里的內(nèi)容driver.find_element_by_id(’kw’).send_keys(Keys.CONTROL, ’x’)time.sleep(3)# 5. ctrl+v 粘貼剪切的內(nèi)容driver.find_element_by_id(’kw’).send_keys(Keys.CONTROL, ’v’)time.sleep(3)# 6. 回車driver.find_element_by_id(’su’).send_keys(Keys.ENTER)time.sleep(3)
在實際的web產(chǎn)品測試中,對于鼠標(biāo)的操作,不單單只有click(),有時候還要用到右擊、雙擊、拖動等操作,這些操作包含在ActionChains類中。
ActionChains類中鼠標(biāo)操作常用方法:
context_click() :右擊 double_click() :雙擊 drag_and_drop() :拖動 move_to_element() :鼠標(biāo)移動到一個元素上舉例:
#cording=gbkimport osfrom selenium import webdriverfrom selenium.webdriver.common.by import By #導(dǎo)入by方法from selenium.webdriver.common.action_chains import ActionChains ##對鼠標(biāo)事件操作current_path=os.path.dirname(__file__)firefox_path=current_path+'/../webdriver/geckodriver.exe'driver=webdriver.Firefox(executable_path=firefox_path)driver.get('http://127.0.0.1/zentao/user-login-L3plbnRhby9teS5odG1s.html')mouse=ActionChains(driver) #創(chuàng)建一個鼠標(biāo)對象# element1=driver.find_element(By.XPATH,'//img[@src=’/zentao/theme/default/images/main/zt-logo.png’]') #Xpath利用屬性定位element1=driver.find_element(By.XPATH,'//img[contains(@src,’images/main/zt-logo.png’)]') #xpath使用包含屬性方法定位mouse.context_click(element1).perform() #執(zhí)行鼠標(biāo)右擊,.perform() 表示執(zhí)行element2=driver.find_element(By.XPATH,'//button[@type=’button’ and @class=’btn’ ]') #多屬性定位mouse.move_to_element(element2).perform() #移動到這個元素上#對元素進(jìn)行截圖driver.find_element(By.XPATH,'//button[@id=’submit’][@type=’submit’]').screensh
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Java commons-httpclient如果實現(xiàn)get及post請求2. 一文帶你徹底理解Java序列化和反序列化3. JS中6個對象數(shù)組去重的方法4. Python基于requests庫爬取網(wǎng)站信息5. vscode運行php報錯php?not?found解決辦法6. python中文本字符處理的簡單方法記錄7. PHP laravel實現(xiàn)導(dǎo)出PDF功能8. Python使用Selenium自動進(jìn)行百度搜索的實現(xiàn)9. PHP利用curl發(fā)送HTTP請求的實例代碼10. 資深程序員:給Python軟件開發(fā)測試的25個忠告!

網(wǎng)公網(wǎng)安備