python鼠标事件 详解_Python selenium键盘鼠标事件实现过程详解
引言
----在實際的web測試工作中,需要配合鍵盤按鍵來操作,webdriver的 keys()類提供鍵盤上所有按鍵的操作,還可以模擬組合鍵Ctrl+a,Ctrl+v等。
舉例:
#cording=gbk
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By #導入by方法
from selenium.webdriver.common.action_chains import ActionChains ##對鼠標事件操作
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)品測試中,對于鼠標的操作,不單單只有click(),有時候還要用到右擊、雙擊、拖動等操作,這些操作包含在ActionChains類中。
ActionChains類中鼠標操作常用方法:
context_click() :右擊
double_click() :雙擊
drag_and_drop() :拖動
move_to_element() :鼠標移動到一個元素上
舉例:
#cording=gbk
import os
from selenium import webdriver
from selenium.webdriver.common.by import By #導入by方法
from selenium.webdriver.common.action_chains import ActionChains ##對鼠標事件操作
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)建一個鼠標對象
# 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í)行鼠標右擊,.perform() 表示執(zhí)行
element2=driver.find_element(By.XPATH,"//button[@type='button' and @class='btn' ]") #多屬性定位
mouse.move_to_element(element2).perform() #移動到這個元素上
#對元素進行截圖
driver.find_element(By.XPATH,"//button[@id='submit'][@type='submit']").screensh
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持龍方網(wǎng)絡(luò)。
總結(jié)
以上是生活随笔為你收集整理的python鼠标事件 详解_Python selenium键盘鼠标事件实现过程详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python3.7界面_Python3.
- 下一篇: mfc指示灯报警显示_奔驰车辆常用指示灯