python + selenium_ mouse event

Introduction-In the actual web product testing, the mouse operation is not only click (), but sometimes right-click, double-click, drag and other operations are also used. These operations are included in the ActionChains class.

1. Common methods of mouse operation in ActionChains:

 context_click (): right-click
 double_click (): double-click
drag_and_drop (): drag      
move_to_element (): mouse moves to an element

 Examples:

#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) #Create a mouse object 
# element1 = driver.find_element (By.XPATH, "// img [@src = '/ zentao / theme / default / images / main / zt-logo.png']" ) #Xpath uses attribute to locate
element1 = driver.find_element (By.XPATH, "// img [contains (@ src, 'images / main / zt-logo.png')]") #xpath uses the include attribute method to locate
mouse. context_click (element1) .perform () #Execute right mouse click, .perform () means execution

element2 = driver.find_element (By.XPATH, "// button [@ type = 'button' and @ class = 'btn']") # Multiattribute positioned 
mouse.move_to_element (element2) .perform () # moves to that element上#Screenshot

elements.
Driver.find_element (By.XPATH, "// button [@ id = 'submit'] [@ type = 'submit']"). Screenshot ('element1.png')

Guess you like

Origin www.cnblogs.com/123anqier-blog/p/12729403.html