软件测试/测试开发丨Selenium 高级控件交互方法

一、使用场景

使用场景 对应事件
复制粘贴 键盘事件
拖动元素到某个位置 鼠标事件
鼠标悬停 鼠标事件
滚动到某个元素 滚动事件
使用触控笔点击 触控笔事件(了解即可)

二、ActionChains解析

  • 实例化类ActionChains,参数为driver实例。
  • 中间可以有多个操作。
  • .perform()代表确定执行。
python 复制代码
ActionChains(self.driver).操作.perform()

三、键盘事件

  • 按下、释放键盘键位
  • 结合send_keys回车

3.1、键盘事件------使用shift键实现大写

  • mouse = ActionChains(self.driver):实例化ActionChains类
  • mouse.key_dowm(keys.SHIFT,ele):按下SHIFT键
  • mouse.send_keys("selenium"):输入字母自动变成大写
  • mouse.perform()

3.2、键盘事件------输入后回车

  • 方法1:.send_keys(Keys.ENTER)
  • 方法2:ActionChains().send_keys(Keys.ENTER).perform()
python 复制代码
self.driver.find_element(By.XPATH, '//input[@aria-label="搜索"]').send_keys(Keys.ENTER)
self.action.send_keys(Keys.ENTER).perform()

3.3、键盘事件-复制粘贴

  • 多系统兼容

    • mac 的复制按钮为 COMMAND
    • windows 的复制按钮为 CONTROL
  • 左箭头:Keys.ARROW_LEFT

  • 按下COMMAND或者CONTROL: key_down(cmd_ctrl)

  • 按下剪切与粘贴按钮: send_keys("xvvvvv")

四、鼠标事件

4.1、双击

  • double_click(元素对象): 双击元素
python 复制代码
    def test_double_click(self):
        self.driver.get("https://vip.ceshiren.com/#/ui_study/frame")
        ele01 = self.driver.find_element(By.XPATH, '//button[@class="el-button el-button--primary"]')
        self.action.double_click(ele01).perform()
        time.sleep(3)

4.2、鼠标事件-拖动元素

  • drag_and_drop(起始元素对象, 结束元素对象): 拖动并放开元素
python 复制代码
    def test_drag_and_drop(self):
        self.driver.get("https://vip.ceshiren.com/#/ui_study/action_chains")
        ele01 = self.driver.find_element(By.ID, "item1")
        ele02 = self.driver.find_element(By.ID, "item3")
        self.action.drag_and_drop(ele01, ele02).perform()

4.3、 鼠标事件-悬浮

  • move_to_element(元素对象): 移动到某个元素
python 复制代码
    def test_move_to_element(self):
        self.driver.get('https://vip.ceshiren.com/#/ui_study/action_chains2')
        ele01 = self.driver.find_element(By.XPATH, '//div [@data-v-3e28c77a and @class="title"]')
        self.action.move_to_element(ele01).perform()
        self.driver.find_element(By.XPATH, '//*[contains(text(),"管理")]').click()

五、滚轮/滚动操作

  • 滚动到元素
  • 根据坐标滚动

注意: selenium 版本需要在 4.2 之后才支持滚动操作

5.1、滚动到元素

  • ActionChains().scroll_to_element().perform()
python 复制代码
    def test_scroll_to_element(self):
        self.driver.get('https://ceshiren.com/')
        ele01 = self.driver.find_element(By.PARTIAL_LINK_TEXT, "jmeter分布式压测创建多节点报错")
        self.action.scroll_to_element(ele01).perform()
        self.action.scroll_by_amount(0, 100).perform()   
        ele01.click()

5.2、根据坐标滚动

  • ActionChains.scroll_by_amount(横坐标,纵坐标)
python 复制代码
    def test_scroll_by_amount(self):
        self.driver.get('https://ceshiren.com/')
        self.action.scroll_by_amount(0, 1000).perform()

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

相关推荐
晓衣21 小时前
2025“獬豸杯”全国电子数据取证竞赛-k8s服务器取证wp
服务器·经验分享·程序人生·网络安全·容器·kubernetes·学习方法
JosieBook1 天前
【程序人生】有梦想就能了不起,就怕你没梦想
程序人生·职场和发展
2501_928094651 天前
Axure RP 9 Mac 交互原型设计
交互·axure·原型设计
Axure组件1 天前
Axure: 分组柱状图1
交互·axure·ux
北岛三生1 天前
Camera tuning flow相机调试流程
图像处理·数码相机·测试工具·模块测试
晋人在秦 老K2 天前
入梦工具箱怎么检测硬件?3步完成CPU-Z跑分测试 硬件检测总出错?图吧工具箱免费功能实测 draw.io 部署指南:私有化流程图服务搭建教程
测试工具·流程图·工具·draw.io
zhangzeyuaaa2 天前
Selenium 超时完全指南:pageLoadTimeout、implicitlyWait 和 scriptTimeout 的深度解析
selenium·测试工具
泛联新安2 天前
如何根据项目需求选择合适的软件测试工具?iUnit智能单元测试平台提供专业化解决方案
c++·测试工具·单元测试
c萱3 天前
软件测试错题笔记
软件测试·数据库·笔记·测试工具·oracle·测试用例
川石教育3 天前
软件测试中的Bug知识总结
软件测试·bug·压力测试·缺陷管理·bug分类