python+selenium_鼠标事件

引言--在实际的web产品测试中,对于鼠标的操作,不单单只有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)  #创建一个鼠标对象
# 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() #执行鼠标右击,.perform() 表示执行

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']").screenshot('element1.png')

猜你喜欢

转载自www.cnblogs.com/123anqier-blog/p/12729403.html