selenium-ActionChains

本文介绍了Selenium的ActionChains和TouchActions模块,用于模拟鼠标和触摸事件。ActionChains提供了点击、双击、拖放等鼠标操作,而TouchActions支持移动端的手势操作。通过调用相应方法并使用perform()执行,这些动作会被存储在队列中,按顺序执行。文中还展示了如何在Python中应用这些操作,包括鼠标移动、拖拽、键盘输入等实际示例。
摘要由CSDN通过智能技术生成

介绍

  • 官方文档:https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains
  • ActionChains:执行PC端的鼠标点击、双击、右键、拖拽等事件
  • TouchActions:模拟PC和移动端的点击、滑动、拖拽、多点触控等多种手势操作

执行原理

  • 调用ActionChains的方法时,不会立即执行,而是将所有的操作,按顺序存放在一个队列里,当你调用perform()方法时,队列中的事件会依次执行

基本用法

  • 生成一个动作action=ActionChains(driver)
  • 动作添加方法1 action.方法1
  • 动作添加方法2 action.方法2
  • 调用perform()方法执行(action.perform())

写法

  • 链式写法
  • 分布写法

鼠标移动到某个元素上

  • action=ActionChains(self.driver)
  • action.move_to_element(element)
  • action.perform()

将一个元素拖拽到某个元素上

  • drag_element
  • drop_element
  • action=ActionChains(self.driver)
  • action.drag_and_drop(drag_element, drop_element) 写法一
  • action.click_and_hold(drag_element).release(drop_element) 写法二
  • action.click_and_hold(drag_element).move_to_element(drop_element) 写法三
  • action.perform()

键盘操作

  • 输入内容:action.send_keys(“hello”)
  • 按下空格:action.send_keys(Keys.SPACE)
  • 按下删除键:action.send_keys(Keys.BACK_SPACE)
  • 快捷键复制: action.key_down(Keys.COMMAND).send_keys(“a”).key_up(Keys.COMMAND)
import time

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys


class TestActionChains:
    def setup(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(5)
        self.driver.maximize_window()

    def teardown(self):
        self.driver.quit()

    def test_click(self):
        self.driver.get("http://sahitest.com/demo/clicks.htm")
        element_click = self.driver.find_element(By.CSS_SELECTOR, 'input[value="click me"]')
        element_double_click = self.driver.find_element(By.CSS_SELECTOR, 'input[value="dbl click me"]')
        element_right_click = self.driver.find_element(By.CSS_SELECTOR, 'input[value="right click me"]')
        action = ActionChains(self.driver)
        # 链式写法
        # action.click(element_click)
        # action.double_click(element_double_click)
        # action.context_click(element_right_click)
        # action.perform()

        # 分布式写法
        action.click(element_click).double_click(element_double_click).context_click(element_right_click).perform()
        time.sleep(3)

    # 鼠标移动到元素上
    def test_move_to_element(self):
        self.driver.get("https://www.baidu.com/")
        ele = self.driver.find_element_by_css_selector("#s-usersetting-top")
        action = ActionChains(self.driver)
        action.move_to_element(ele)
        action.perform()
        time.sleep(3)

    # 将一个元素拖拽到另一个元素上
    def test_drag_drop(self):
        self.driver.get("http://sahitest.com/demo/dragDropMooTools.htm")
        dragger_element = self.driver.find_element_by_id('dragger')
        drop_element = self.driver.find_element_by_xpath("//body/div[2]")
        action = ActionChains(self.driver)
        # 写法一
        # action.drag_and_drop(dragger_element, drop_element)
        # 写法二
        # action.click_and_hold(dragger_element).release(drop_element)
        # 写法三
        action.click_and_hold(dragger_element).move_to_element(drop_element)

        action.perform()
        time.sleep(3)

    def test_key(self):
        self.driver.get("http://sahitest.com/demo/label.htm")

        user_name_element = self.driver.find_element_by_xpath("/html/body/label[1]/input")

        # 将光标定位到文本框中国
        user_name_element.click()

        action = ActionChains(self.driver)

        # 在本文框中输入内容
        action.send_keys("hello")
        action.pause(3)

        # 键盘按下空格
        action.send_keys(Keys.SPACE)
        action.pause(3)

        # 在文本框中输入内容
        action.send_keys("world!")
        action.pause(3)

        # 键盘按下删除键
        action.send_keys(Keys.BACK_SPACE)
        action.pause(3)

        # 鼠标键按下command+c,当前使用的电脑是mac
        action.key_down(Keys.COMMAND).send_keys("a").key_up(Keys.COMMAND)
        action.pause(3)

        action.perform()

        time.sleep(3)

document.querySelectorAll(’*’)

可以使用写法 $(’*’) 是因为页面使用了 JQuery 这个库

css表达式写法一致

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值