前言:在实际场景中,会有单击、长时间单击、双击、右键、拖拽等鼠标操作,selenium提供了名为ActionChains的类来处理这些操作。

一、ActionChains类的引入

在使用鼠标操作前,需要引入ActionChains,如下:

from selenium.webdriver.common.action_chains import ActionChains

二、ActionChains类的方法列表

以下是ActionChains类的方法,总的来说有三类:点击、拖拽、移动。值得注意的是,所有的操作都需要通过perform()来执行,所以要想操作正常执行,不要忘记添加perform方法。

from selenium.webdriver.common.action_chains import ActionChains

# 创建鼠标操作对象
action = ActionChains(driver)

click(on_element=None)             #单击鼠标左键
click_and_hold(on_element=None)    #点击鼠标左键,按住不放
context_click(on_element=None)       #点击鼠标右键
double_click(on_element=None)        #双击鼠标左键
drag_and_drop(source, target)        #拖拽到某个元素然后松开
drag_and_drop_by_offset(source, xoffset, yoffset)    #拖拽到某个坐标然后松开
move_by_offset(xoffset, yoffset)     #鼠标移动到距离当前位置(x,y)
move_to_element(to_element)          #鼠标移动到某个元素
move_to_element_with_offset(to_element, xoffset, yoffset)    #将鼠标移动到距某个元素多少距离的位置
release(on_element=None)             #在某个元素位置松开鼠标左键
perform()                            #执行链中的所有动作

三、点击、拖拽、移动的实现

import time
import pytest
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By

class TestMouse:
    def setup_class(self):
        self.driver = webdriver.Chrome()
        # 创建鼠标操作对象
        self.action = ActionChains(self.driver)

    def teardown_class(self):
        self.driver.close()
        self.driver.quit()

    def test_mouse_click(self): # 点击
        self.driver.get("https://sahitest.com/demo/clicks.htm")
        # 定位元素
        sli_ele  = self.driver.find_element(By.CSS_SELECTOR,'input[value="dbl click me"]')
        sli_ele2  = self.driver.find_element(By.CSS_SELECTOR,'input[value="click me"]')
        sli_ele3  = self.driver.find_element(By.CSS_SELECTOR,'input[value="right click me"]')

        # 双击鼠标左键
        self.action.double_click(sli_ele).perform()
        time.sleep(2)

        # 单击鼠标左键
        self.action.click(sli_ele2).perform()
        time.sleep(2)

        # 点击鼠标右键
        self.action.context_click(sli_ele3).perform()
        time.sleep(2)

    def test_mouse_drag(self): # 拖拽
        self.driver.get('https://sahitest.com/demo/dragDropMooTools.htm')
        sli_ele1 = self.driver.find_element(By.ID,'dragger')
        sli_ele2 = self.driver.find_element(By.XPATH, '/html/body/div[2]')

        self.action.drag_and_drop(sli_ele1,sli_ele2).perform()

        time.sleep(2)

    def test_mouse_move(self):  # 悬停
        self.driver.get('https://sahitest.com/demo/mouseover.htm')
        sli_ele1 = self.driver.find_element(By.XPATH,'/html/body/a[1]/span')
        sli_ele2 = self.driver.find_element(By.XPATH,'/html/body/form/input[1]')
        sli_ele3 = self.driver.find_element(By.XPATH,'/html/body/div/div')

        self.action.move_to_element(sli_ele1).perform()
        time.sleep(2)
        self.action.move_to_element(sli_ele2).perform()
        time.sleep(2)
        self.action.move_to_element(sli_ele3).perform()
        time.sleep(2)

if __name__ == '__main__':
    pytest.main(['-s'])


最后修改:2022 年 07 月 08 日
如果觉得我的文章对你有用,请随意赞赏