【ActionChains方法列表】:
move_to_element(to_element) ——鼠标移动到某个元素
click(on_element=None) ——单击鼠标左键
click_and_hold(on_element=None) ——点击鼠标左键,不松开
double_click(on_element=None) ——双击鼠标左键
context_click(on_element=None) ——点击鼠标右键
drag_and_drop(source, target) ——拖拽到某个元素然后松开
drag_and_drop_by_offset(source, xoffset, yoffset) ——拖拽到某个坐标然后松开
move_by_offset(xoffset, yoffset) ——鼠标从当前位置移动到某个坐标
move_to_element_with_offset(to_element, xoffset, yoffset) ——移动到距某个元素(左上角坐标)多少距离的位置
key_down(value, element=None) ——按下某个键盘上的键
key_up(value, element=None) ——松开某个键
send_keys_to_element(element, *keys_to_send) ——发送某个键到指定元素
send_keys(*keys_to_send) ——发送某个键到当前焦点的元素
perform() ——执行链中的所有动作
release(on_element=None) ——在某个元素位置松开鼠标左键
示例Demo:
# -*- coding: utf-8 -*-
# @Time : 2021/9/3 15:03
# @Author : CuiShuangqi
# @File : selenium_ActionChains.py
import unittest
from time import sleep
from selenium import webdriver
from selenium.webdriver import ActionChains
class TestSeleniumActionChains(unittest.TestCase):
def setUp(self) -> None:
self.driver = webdriver.Chrome()
self.driver.get("https://www.baidu.com")
self.driver.maximize_window()
self.driver.implicitly_wait(10)
sleep(3)
def tearDown(self) -> None:
sleep(5)
self.driver.quit()
# 鼠标悬浮在元素上
def test_001(self):
# 百度首页右上角"设置"按钮
print("鼠标悬浮在元素上")
ele = self.driver.find_element_by_id("s-usersetting-top")
action = ActionChains(self.driver)
action.move_to_element(ele).perform()
# 鼠标左键单击元素
def test_002(self):
# 百度首页右上角"设置"按钮
print("鼠标左键单击元素")
ele = self.driver.find_element_by_id("s-usersetting-top")
action = ActionChains(self.driver)
action.click(ele).perform()
# 鼠标左键单击元素不松开
def test_003(self):
# 百度首页右上角"设置"按钮
print("鼠标左键单击元素不松开")
ele = self.driver.find_element_by_id("s-usersetting-top")
action = ActionChains(self.driver)
action.click_and_hold(ele).perform()
# 鼠标左键双击元素
def test_004(self):
# 百度首页右上角"设置"按钮
print("鼠标左键双击元素")
ele = self.driver.find_element_by_id("s-usersetting-top")
action = ActionChains(self.driver)
action.double_click(ele).perform()
# 鼠标右键单击元素
def test_005(self):
# 百度首页右上角"设置"按钮
print("鼠标右键单击元素")
ele = self.driver.find_element_by_id("s-usersetting-top")
action = ActionChains(self.driver)
action.context_click(ele).perform()
# 鼠标左键拖拽某个元素至某个元素松开
def test_006(self):
# 百度首页右上角"设置"按钮
print("鼠标左键拖拽某个元素松开")
# 源位置
ele_source = self.driver.find_element_by_xpath("//*[@id='hotsearch-content-wrapper']/li[1]/a/span[1]")
# 目标位置
ele_target = self.driver.find_element_by_id("s-usersetting-top")
action = ActionChains(self.driver)
action.drag_and_drop(ele_source, ele_target).perform()
if __name__ == '__main__':
unittest.main()
网友评论