• Selenium-鼠标和键盘操作


    1、鼠标操作

    常见的鼠标操作有:点击、右击、双击、悬停、拖拽等,对于这些鼠标操作Selenium都封装了相应的操作方法。

    1.1 为什么要操作鼠标?

    现在Web产品中存在丰富的鼠标交互方式,作为一个Web自动化测试框架,需要应对这些鼠标操作的应用场景。

    1.2 鼠标操作的方法

    说明:在Selenium中将操作鼠标的方法封装在ActionChains类中 
    实例化对象: 
    	action = ActionChains(driver) 
    方法:
    	1. context_click(element) 右击 --> 模拟鼠标右键点击效果 
    	2. double_click(element) 双击 --> 模拟鼠标双击效果 
    	3. drag_and_drop(source, target) 拖动 --> 模拟鼠标拖动效果 
    	4. move_to_element(element) 悬停 --> 模拟鼠标悬停效果 
    	5. perform() 执行 --> 此方法用来执行以上所有鼠标操作 
    
    
    为了更好的学习其他方法,我们先学习perform()执行方法,因为所有的方法都需要执行才能生效
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    1.3 鼠标执行-perform()

    说明:在ActionChains类中所有提供的鼠标事件方法,在调用的时候所有的行为都存储在ActionChains对象中, 
    而perform()方法就是真正去执行所有的鼠标事件。
    
    强调:必须调用perform()方法才能执行鼠标事件
    
    • 1
    • 2
    • 3
    • 4

    1.4 鼠标右击-context_click()

    说明:对于点击鼠标右键,如果弹出的是浏览器默认的菜单,Selenium没有提供操作菜单选项的方法; 
    
    	如果是自定义的右键菜单,则可以通过元素定位来操作菜单中的选项。
    
    • 1
    • 2
    • 3

    1.4.1 练习-代码实现关键点分析

    # 需求:打开百度,在搜索框上点击鼠标右键
    
    1. 导包:from selenium.webdriver.common.action_chains import ActionChains 
    2. 实例化ActionChains对象:action = ActionChains(driver) 
    3. 调用右键方法:action.context_click(element) 
    4. 执行:action.perform()
    
    # 快速导包: Alt+Enter
    # 快速调整格式: Ctrl+Alt+L
    
    
    # 导包
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    
    # 获取浏览器驱动对象
    driver = webdriver.Firefox()
    
    url = "https://www.baidu.com"
    # 实例化 ActionChains对象
    action = ActionChains(driver)
    
    
    driver.get(url)
    
    # 使用CSS id选择器获取搜索框
    kw = driver.find_element_by_css_selector("#kw")
    # 右击搜索框并执行
    action.context_click(kw).perform()
    
    driver.quit()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    1.5 鼠标双击-double_click()

    说明:模拟双击鼠标左键操作
    
    • 1

    1.5.1 练习

    # 需求:打开百度,在搜索框上输入CSDN,双击鼠标左键,选中CSDN
    
    # 导包
    from time import sleep
    
    from selenium import webdriver
    
    
    # 获取浏览器驱动对象
    from selenium.webdriver import ActionChains
    
    driver = webdriver.Firefox()
    
    url = "https://www.baidu.com"
    # 实例化 ActionChains对象
    action = ActionChains(driver)
    
    
    driver.get(url)
    
    # 使用CSS id选择器获取搜索框
    kw = driver.find_element_by_css_selector("#kw")
    
    # 搜索框中输入CSDN
    kw.send_keys("CSDN")
    
    driver.find_element_by_css_selector("[value= '百度一下']").click()
    # 双击左键选中CSDN
    action.double_click(kw).perform()
    
    sleep(5)
    driver.quit()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    1.6 鼠标拖拽-drag_and_drop()

    说明:模拟鼠标拖动动作,选定拖动源元素释放到目标元素
    
    拖动关键点分析:
    	1. 源元素 source = driver.find_element_by_id(xxx) 
    	2. 目标元素 target = driver.find_element_by_id(xxx) 
    	3. 调用方法 action.drag_and_drop(source, target).perform()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    1.7 鼠标悬停-move_to_element()

    说明:模拟鼠标悬停在指定的元素上
    
    • 1

    1.7.1 练习

    # 需求:打开百度,在搜索框上输入CSDN,悬停
    
    # 导包
    from time import sleep
    
    from selenium import webdriver
    
    # 获取浏览器驱动对象
    from selenium.webdriver import ActionChains
    
    driver = webdriver.Firefox()
    
    url = "https://www.baidu.com"
    # 实例化 ActionChains对象
    action = ActionChains(driver)
    
    driver.get(url)
    
    # 使用CSS id选择器获取搜索框
    kw = driver.find_element_by_css_selector("#kw")
    
    
    move = driver.find_element_by_css_selector('.aging-entry-inner')
    # 悬停
    action.move_to_element(move).perform()
    
    sleep(5)
    driver.quit()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    2.键盘操作

    思考:如何实现复制、粘贴的操作?

    说明:
    	1). 模拟键盘上一些按键或者组合键的输入 如:Ctrl+C 、Ctrl+V; 
    	2). Selenium中把键盘的按键都封装在Keys类中
    
    • 1
    • 2
    • 3

    2.1 Keys类

    导包:from selenium.webdriver.common.keys import Keys
    
    • 1

    2.2 常用的键盘操作

    1. send_keys(Keys.BACK_SPACE) 			删除键(BackSpace) 
    2. send_keys(Keys.SPACE) 				空格键(Space) 
    3. send_keys(Keys.TAB) 					制表键(Tab) 
    4. send_keys(Keys.ESCAPE) 				回退键(Esc) 
    5. send_keys(Keys.ENTER) 				回车键(Enter) 
    6. send_keys(Keys.CONTROL,'a') 			全选(Ctrl+A) 7. send_keys(Keys.CONTROL,'c') 复制(Ctrl+C) 
    
    
    提示:以上方法就不一个一个讲解了,因为调用方法都一样;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.3 案例

    需求:打开注册A页面,完成以下操作 
    1). 输入用户名:admin1,暂停2秒,删除1 
    2). 全选用户名:admin,暂停23). 复制用户名:admin,暂停24). 粘贴到密码框
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.4 示例代码

    # 定位用户名 
    element = driver.find_element_by_id("userA") 
    # 输入用户名 
    element.send_keys("admin1") 
    # 删除1 
    element.send_keys(Keys.BACK_SPACE) 
    # 全选 
    element.send_keys(Keys.CONTROL, 'a') 
    # 复制 
    element.send_keys(Keys.CONTROL, 'c') 
    # 粘贴
    driver.find_element_by_id('passwordA').send_keys(Keys.CONTROL, 'v')
    
    # 导包
    from selenium import webdriver
    from time import sleep
    from selenium.webdriver.common.keys import Keys
    
    
    # 获取浏览器驱动对象
    driver = webdriver.Firefox()
    # 打开url
    url = r"D:\web自动化素材\课堂素材\注册A.html"
    driver.get(url)
    """
        目标:学习使用基于selenium完成键盘操作
        案例:
            1. 输入用户名admin1
            2. 删除 1 
            3. 全选 用户名 "admin"  Ctrl+A
            4. 复制 admin Ctrl+C
            5. 粘贴 将复制的admin粘贴到密码框
        分析:
            1. 导包 Keys
            2. 调用 send_keys(Keys.XXX,'a')
            
    """
    # 定位 用户名
    username = driver.find_element_by_css_selector("#userA")
    # 输入 admin1
    username.send_keys("admin1")
    sleep(2)
    # 删除1
    username.send_keys(Keys.BACK_SPACE)
    
    sleep(2)
    
    # 全选 admin Ctrl+a
    username.send_keys(Keys.CONTROL, "a")
    sleep(2)
    
    # 复制 Ctrl+c
    username.send_keys(Keys.CONTROL, "c")
    sleep(2)
    
    # 定位密码框 并执行 Ctrl+v
    driver.find_element_by_css_selector("#passwordA").send_keys(Keys.CONTROL, "v")
    
    # 暂停 2
    sleep(2)
    # 关闭驱动对象
    driver.quit()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
  • 相关阅读:
    华为机试HJ52
    华为OD机试 - 查找接口成功率最优时间段 - 回溯(Java 2023 B卷 100分)
    Excel 数据透视表教程大全之 07 数据透视表使用日期字段自动按月、年对销售数据进行分组(教程含数据)
    SpringBoot 接口访问频率限制
    vue3插槽、具名插槽、作用域插槽-足够入门了!
    C#基于BytesIO程序包的TCP Client客户端窗体程序
    es(Elasticsearch)安装使用(03ik分词器安装篇)
    踩坑记:JSON.parse和JSON.stringify
    《Effective C++》知识点(1)--让自己习惯C++
    传输层协议——UDP
  • 原文地址:https://blog.csdn.net/weixin_44244493/article/details/126681118