The method of mouse operation in Webdriver encapsulated in the Actionchains class
Common methods provided by the Actionchains class:
Perform (): Performs the storage behavior in all Actionchains
Contextclick () Right-click
Double_click () Double-click
Drag_and_drop () drag
Move_to_element () mouse hover
Mouse Right-click event
in the following code: from selenium.driver import actionchains Import Actionchains class that provides mouse action
Actionchains (driver) calls the Actionchains class, passing the browser driver driver as a parameter
The Context_click (Right_click) method is used to simulate the right mouse button operation, which requires the specified element to be positioned when called
Perform () performs all the storage behavior in the Actionchains, committing to the entire operation.
2. Mouse hover Move_to_element () method can simulate mouse hover action 3. Mouse double-click operation using Double_click function 4. Mouse drag-and-drop Operation Drag_and_drop (Source,target) hold down the left mouse button on the source element, And then move to the target to release the source: mouse Drag the target element of the mouse to release from selenium import webdriverfrom time import *from Selenium.webdriver.common.action_chains Import actionchainsdriver = 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] ') #鼠标右击ActionChains (driver). Context_click (Right_click). Perform () #鼠标悬停 # Actionchains (Driver). Move_to_element (Right_click). Perform () #鼠标双击ActionChains (driver). Double_click (Right_click) . Perform () sleep (3) driver.quit ()
Selenium Learning: Mouse events