selenium- simulate mouse

You need to import the package:

from selenium.webdriver import ActionChains

First, right-click simulation

ActionChains(self.driver).context_click(xxx).perform()  

# coding=UTF-8
#19.模拟鼠标右键
import sys
reload(sys)
sys.setdefaultencoding('utf8')
from selenium import webdriver
import unittest
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains

class Case18(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome()

    def test_simulateASingleKey(self):
        url = " Https://www.sogou.com " 
        self.driver.get (URL) 
        Element = self.driver.find_element_by_id ( " Query " ) 
        element.send_keys ( " Selenium " ) 
        the time.sleep ( 2 ) 
        element.send_keys ( Keys.CONTROL, ' A ' )   # C TRL + A select all content input box 
        time.sleep (2 ) 
        element.send_keys (Keys.CONTROL, ' X ' ) # Ctrl + X shear content input box 
        time.sleep (2 )
       ActionChains (self.driver) .context_click (Element) .perform ()  # Right-click-click 
        the time.sleep (2 )
        ActionChains (self.driver) .send_keys ( ' P ' ) .perform ()  # send command adhesive, character P represents applicators (only IE browser) 
        the time.sleep (2 ) 

    DEF the tearDown (Self): 
        self.driver.quit () 

IF  the __name__ == ' __main__ ' : 
    unittest.main ()

 

Second, the simulation of the left mouse button is pressed and released

ActionChains (self.driver) .click_and_hold (xxx) .perform () - on the left mouse button pressed down and holding element xxx
ActionChains (self.driver) .release (xxx) .perform () - on element xxx release the left mouse button has been pressed

 

Third, keep the mouse hovers over an element

ActionChains (self.driver) .move_to_element (xxx) .perform () - Hover element to the xxx

 

 

Guess you like

Origin www.cnblogs.com/erchun/p/11800806.html