Cyan Blog Cyan Blog
首页
  • Java (opens new window)
  • JUC (opens new window)
  • JVM (opens new window)
  • Redis

    • Redis安装 (opens new window)
    • Redis基础 (opens new window)
    • Redis实战 (opens new window)
    • Redis集群安装 (opens new window)
    • Redis分布式缓存 (opens new window)
    • Redis多级缓存 (opens new window)
    • Redis原理 (opens new window)
  • 管理工具

    • Maven (opens new window)
    • Git (opens new window)
  • SSM

    • Spring (opens new window)
    • SpringBoot (opens new window)
    • Mybatis (opens new window)
    • MybatisPlus (opens new window)
  • 微服务

    • Docker (opens new window)
    • RabbitMQ (opens new window)
    • SpringCloud (opens new window)
    • Dubbo (opens new window)
    • MongoDB (opens new window)
    • Zookeeper (opens new window)
  • Java面试题 (opens new window)
  • JUC面试题 (opens new window)
  • JVM面试题 (opens new window)
  • Linux面试题 (opens new window)
  • SQL面试题 (opens new window)
  • Maven面试题 (opens new window)
  • Redis面试题 (opens new window)
  • SSM面试题 (opens new window)
  • SpringCloud面试题 (opens new window)
  • Linux (opens new window)
  • C++ (opens new window)
  • 数据库

    • MySQL (opens new window)
    • NoSQL (opens new window)
  • 软件测试

    • 软件测试 (opens new window)
  • 加密解密 (opens new window)
  • bilibili字幕提取 (opens new window)
  • 道理 (opens new window)
  • 关于博主

    • Github (opens new window)
    • CSDN (opens new window)
  • 关于本站

    • 如何搭建博客网站 (opens new window)
首页
  • Java (opens new window)
  • JUC (opens new window)
  • JVM (opens new window)
  • Redis

    • Redis安装 (opens new window)
    • Redis基础 (opens new window)
    • Redis实战 (opens new window)
    • Redis集群安装 (opens new window)
    • Redis分布式缓存 (opens new window)
    • Redis多级缓存 (opens new window)
    • Redis原理 (opens new window)
  • 管理工具

    • Maven (opens new window)
    • Git (opens new window)
  • SSM

    • Spring (opens new window)
    • SpringBoot (opens new window)
    • Mybatis (opens new window)
    • MybatisPlus (opens new window)
  • 微服务

    • Docker (opens new window)
    • RabbitMQ (opens new window)
    • SpringCloud (opens new window)
    • Dubbo (opens new window)
    • MongoDB (opens new window)
    • Zookeeper (opens new window)
  • Java面试题 (opens new window)
  • JUC面试题 (opens new window)
  • JVM面试题 (opens new window)
  • Linux面试题 (opens new window)
  • SQL面试题 (opens new window)
  • Maven面试题 (opens new window)
  • Redis面试题 (opens new window)
  • SSM面试题 (opens new window)
  • SpringCloud面试题 (opens new window)
  • Linux (opens new window)
  • C++ (opens new window)
  • 数据库

    • MySQL (opens new window)
    • NoSQL (opens new window)
  • 软件测试

    • 软件测试 (opens new window)
  • 加密解密 (opens new window)
  • bilibili字幕提取 (opens new window)
  • 道理 (opens new window)
  • 关于博主

    • Github (opens new window)
    • CSDN (opens new window)
  • 关于本站

    • 如何搭建博客网站 (opens new window)
  • 设计模式

  • 软件测试

  • 软件测试实验

    • 软件测试
    • WebDriver⭐⭐
    • 定位⭐⭐
    • 浏览器操作⭐
    • 元素基本操作⭐⭐
    • 模拟键鼠操作⭐⭐
      • 消息框操作⭐
      • 切换浏览器窗口⭐
      • SoftwareTest⭐
      • SoftwareTest
      • SoftwareTest
      • SoftwareTest
      • SoftwareTest
    • 计算机
    • 软件测试实验
    2025-03-17
    0
    0
    目录

    模拟键鼠操作⭐⭐

    # 模拟键盘操作

    • selenium:提供了完整的模拟键盘操作事件,模拟键盘的操作需要先导入键盘操作专用类Keys:
      • from selenium.webdriver.common.keys import Keys
    • 模拟按键操作的语法:
      • 元素对象.send keys(Keys.常量)
    • 说明:这些常量作为send_keys.方法的参数使用即可模拟用户对于该按键的点击操作,如果模拟多个按键组合操作,就给send keys,方法添加多个参数即可。
    编号 常量 按键 编号 常量 按键
    1 ENTER 回车键 9 ESCAPE Esc键
    2 BACKSPACE 退格键 10 HOME 定位行首
    3 SHIFT Shift键 11 END 定位行尾
    4 CONTROL Ctrl键 12 F1--F12 F1--F12键
    5 TAB tab键 13 NUMPAD0----NUMPAD9 小键盘的0---9数字
    6 ALT Alt键 14 ARROW_LEFT\ARROW_UP\ARROW_RIGHT\ARROW_DOWN 方向键(↑→↓)
    7 INSERT 插入键 15 PAGE_UP\PAGE_DOWN 上一页\下一页
    8 DELETE 删除键 16 ADD\SUBTRACT\MULTIPLY\DIVIDE\EQUALS 加减\乘\除\等于
    • 删除最后一个内容
    kw=driver.find_element(By.ID,"keyword")
    kw.send_keys("806")
    kW.send_keys(Keys.ENTER)
    
    kw=driver.find_element(By.ID,"keyword")
    kw.click()
    kw.send_keys(Keys.END)
    kW.send_keys(Keys.ARROW_LEFT)
    
    kw.send_keys(Keys.DELETE)
    
    kW.send_keys(Keys.ENTER)
    
    • 全选复制粘贴
    tex1=driver.find_element(By.NAME,"username")
    tex1.send_keys("vip")
    tex1.send_keys(Keys.CONTROL,"a")#全选
    tex1.send_keys(Keys.CONTROL,"c")#复制
    tex2=driver.find_element(By.NAME,"password")
    tex2.send_keys(Keys.CONTROL,"v")#粘贴
    tex2.send_keys(Keys.ENTER)
    
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from time import sleep
    from selenium.webdriver.common.keys import Keys
    driver = webdriver.Firefox()
    driver.implicitly_wait(5)
    driver.get('http://localhost:8080/upload/index.php')
    driver.find_element(By.LINK_TEXT, '留言板').click()
    inp=driver.find_element(By.NAME, 'user_email')
    inp.send_keys('vip@ecshop.com')
    inp.send_keys(Keys.HOME)
    sleep(3)
    inp.send_keys(Keys.SHIFT,Keys.ARROW_RIGHT,Keys.ARROW_RIGHT,Keys.ARROW_RIGHT)
    sleep(3)
    inp.send_keys(Keys.CONTROL,'c')
    driver.find_element(By.NAME, 'msg_title').send_keys(Keys.CONTROL,'v')
    te=driver.find_element(By.NAME, 'msg_content')
    te.send_keys('我是')
    te.send_keys(Keys.CONTROL,'v')
    te.send_keys(Keys.ENTER)
    te.send_keys('请问有优惠码?')
    sleep(3)
    driver.find_element(By.NAME, 'msg_title').send_keys(Keys.ENTER)
    sleep(3)
    driver.quit()
    

    # 模拟鼠标操作

    在实际场景中,会有单击、长时间单击、双击、右击、拖放、移动等鼠标操作,或在当前光标位置的按键输入或鼠标操作。 selenium提供了名为ActionChains的类来处理这些操作,我们一般翻译为“操作链”或“动作链”。

    导入ActionChains类:from selenium.webdriver.common.action_chains import ActionChains

    ActionChains类使用前需要先实例化:变量=ActionChains(driver)

    编号操作方法 功能说明
    1* click(on_element=None) 单击鼠标左键
    2 * click_and_hold(on_element=None) 点击鼠标左键,不松开
    3* context_click(on_element=None) 点击鼠标右键
    4 * double_click(on_element=None) 双击鼠标左键
    5 * drag_and_drop(source, target) 拖拽到某个元素位置后松开
    6 drag_and_drop_by _offset(source, xoffset, yoffset) 拖拽到某个坐标后松开
    7 key_down(value, element=None) 按下某个键盘上的键
    8 key_up(value, element=None) 松开某个键
    9 * move_to_element(to_element) 鼠标移动到某个元素
    10 move_by_offset(xoffset, yoffset) 鼠标从当前位置移动到某个坐标, 一般很少用位置关系来移动鼠标
    11 move_to_element_with_offset(to_element, xoffset, yoffset) 移动到距某个元素 (左上角坐标) 多少距离的位置
    12 * release(on_element=None) 在某个元素位置松开鼠标左键
    13 * *send_keys(keys_to_send) 发送某个键到当前焦点位置的元素
    14 send_keys_to_element(element, *keys_to_send) 发送某个键到指定元素
    15 * perform() 执行链中的所有动作

    # ActionChains基本用法

    两种调用方法,效果相同。

    1. 链式写法: ActionChains(driver).方法1(参数).方法2(参数).perform()
    2. 分步写法: 变量=ActionChains(driver) 变量.方法1(参数) 变量.方法2(参数) 变量.perform()
    • 操作链
    double_cli = driver.find_element(By.XPATH, '//input[@value="dbl click me"]')
    cli = driver.find_element(By.XPATH, '//input [@value="click me"]')
    right_cli = driver.find_element(By.XPATH, '//input[@value="right click me"]')
    # ActionChains(driver).double_click(double_cli).click(cli).context_click(right_cli).perform()
    action = ActionChains(driver)
    action.click(cli)
    
    action.double_click(double_cli)
    
    action.context_click(right_cli)
    
    action.perform()
    
    • 使用send_keys模拟tab键
    t=driver.find_element(By.ID,"keyword")
    ActionChains(driver).click(t).perform()
    
    ActionChains(driver).send_keys(Keys.TAB).perform()
    
    ActionChains(driver).send_keys(Keys.TAB).perform()
    
    ActionChains(driver).send_keys(Keys.ENTER).perform()
    
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.common.action_chains import ActionChains
    from time import sleep
    driver = webdriver.Firefox()
    driver.implicitly_wait(5)
    driver.get('http://localhost:8080/upload/index.php')
    driver.find_element(By.XPATH,'//a[@href="user.php"]').click()
    
    username=driver.find_element(By.NAME,'username')
    sleep(3)
    action=ActionChains(driver)
    action.move_to_element(username).click().send_keys('vip').send_keys(Keys.TAB).perform()
    sleep(2)
    action.send_keys('vip').send_keys(Keys.TAB).perform()
    sleep(2)
    action.send_keys(Keys.ENTER).perform()
    sleep(5)
    driver.quit()
    
    #软件测试
    上次更新: 2025/5/2 14:40:28
    元素基本操作⭐⭐
    消息框操作⭐

    ← 元素基本操作⭐⭐ 消息框操作⭐→

    最近更新
    01
    项目优化
    05-06
    02
    项目优化
    05-06
    03
    延迟消息
    05-05
    更多文章>
    Theme by Vdoing | Copyright © 2025-2025 Cyan Blog
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式