美文网首页
用Selenium实现鼠标单击、双击、右击

用Selenium实现鼠标单击、双击、右击

作者: 吱吱菌啦啦 | 来源:发表于2022-04-28 11:09 被阅读0次

    官方文档:https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains

    • ActionChains:执行pc端的鼠标点击,双击,右击,拖拽等事件。
    • 原理:调用ActionChains()方法,不会立即执行,而是将所有的操作按执行顺序放在队列中,调用perform()方法时,队列中的事件会依次执行。
    • 基本用法:
      • 生成一个动作action = ActionChains(dirver)
      • 动作添加方法1 action.方法1
      • 动作添加方法2 action.方法2
      • 调用perform()方法执行(action.perform())
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    
    class TestActionChains():
        def setup(self):
            self.driver = webdriver.Chrome()
            # 隐式等待
            self.driver.implicitly_wait(5)
            self.driver.get("https://sahitest.com/demo/clicks.htm")
    
        def teardown(self):
            self.driver.quit()
    
        def test_click(self):
            """
            单击、双击、右击
            :return:
            """
            # 定位元素
            element_click = self.driver.find_element(By.XPATH, '/html/body/form/input[3]')
            element_double_click = self.driver.find_element(By.XPATH, '/html/body/form/input[2]')
            element_right_click = self.driver.find_element(By.XPATH, '/html/body/form/input[4]')
            # 调用ActionChains方法,生成一个动作
            action = ActionChains(self.driver)
            # 给动作添加方法
            action.click(element_click)
            action.double_click(element_double_click)
            action.context_click(element_right_click)
            # 调用perform()方法执行
            action.perform()
    

    相关文章

      网友评论

          本文标题:用Selenium实现鼠标单击、双击、右击

          本文链接:https://www.haomeiwen.com/subject/sjaryrtx.html