• Web自动化测试进阶 —— Selenium模拟鼠标操作


    鼠标操作事件

    在实际的web产品测试中,对于鼠标的操作,不单单只有click(),有时候还要用到右击、双击、拖动等操作,这些操作包含在ActionChains类中。

    ActionChains类中鼠标操作常用方法:

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

    context_click():右击
    double_click():双击
    drag_and_drop():拖动
    move_to_element():鼠标移动到一个元素上
    click_and_hold():按下鼠标左键在一个元素上(长按)

    常用的链条命令

    pause():停留、click():点击、release():释放、perform():执行

    ActionChains(driver).move_to_element(元素对象).pause(秒).click(元素对象).release(元素对象).perform()

    代码如下:

    1. import os
    2. from selenium import webdriver
    3. from selenium.webdriver.common.by import By
    4. from selenium.webdriver.common.action_chains import ActionChains
    5. current_path = os.path.dirname(os.path.abspath(__file__)) # 当前路径
    6. driver_path = os.path.join(current_path,'../webdriver/chromedriver.exe') # driver路径
    7. driver = webdriver.Chrome(executable_path=driver_path) # Firefox,Ie等
    8. driver.get('https://www.baidu.com/') # 打开网站
    9. # 右击操作 context_click()
    10. element_obj = driver.find_element(By.XPATH,'//input[@id="su"]') # 右击百度一下
    11. mouse_obj = ActionChains(driver)
    12. mouse_obj.context_click(element_obj).perform() # perform执行操作
    13. # 点击操作 click()
    14. element_obj = driver.find_element(By.XPATH,'//a[text()="hao123"]')
    15. mouse_obj = ActionChains(driver)
    16. mouse_obj.click(element_obj).release(element_obj).perform() # 点击hao123
    17. # 长按操作 click_and_hold()
    18. element_obj = driver.find_element(By.XPATH,'//a[text()="hao123"]')
    19. mouse_obj = ActionChains(driver)
    20. mouse_obj.click_and_hold(element_obj).pause(10).release(element_obj).perform() #长按 hao123 10秒后松开
    21. # 鼠标移动到一个元素 move_to_element()
    22. e1 = driver.find_element(By.XPATH,'//a[@name="tj_briicon"]')
    23. e2 = driver.find_element(By.XPATH,'//a[@name="tj_zhidao"]')
    24. mouse_obj = ActionChains(driver)
    25. mouse_obj.move_to_element(e1).pause(3).click(e2).release(e2).perform() # 链条命令 移动到 更多 元素上停顿3秒,然后点击 知道 元素

    键盘操作事件

    在实际的web测试工作中,需要配合键盘按键来操作,webdriver的keys()类提供键盘上所有按键的操作,还可以模拟组合键Ctrl_a,Ctrl+c/v等。

    前置条件:导入Keys类

    from selenium.webdriver.common.keys import Keys

    页面上的键盘操作(从搜索框中按两下tab键)

    driver.find_element(By.XPATH,'//input[@id="kw"]').click()
    ActionChains(driver).send_keys(Keys.TAB).pause(1).send_keys(Keys.TAB).perform()

    组合键操作 ctrl+a、ctrl+c、ctrl+v

    driver.find_element(By.XPATH,'//input[@id="kw"]').send_keys('python')
    ActionChains(driver).key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()

    备注:

    1、在使用修饰键的时候需要key_down()和key_up()方法

      修饰键包含ctrl、alt、shift

    2、类似alt+F4 ctrl+alt+delete不能使用,这里的组合键只针对网页生效的

    代码如下:

    1. import os
    2. from selenium import webdriver
    3. from selenium.webdriver.common.by import By
    4. from selenium.webdriver.common.action_chains import ActionChains
    5. from selenium.webdriver.common.keys import Keys
    6. current_path = os.path.dirname(os.path.abspath(__file__)) # 当前路径
    7. driver_path = os.path.join(current_path,'../webdriver/chromedriver.exe') # driver路径
    8. driver = webdriver.Chrome(executable_path=driver_path) # Firefox,Ie等
    9. driver.get('https://www.baidu.com/') # 打开网站
    10. # 页面上的键盘操作 从搜索框中按两下tab键
    11. driver.find_element(By.XPATH,'//input[@id="kw"]').click()
    12. ActionChains(driver).send_keys(Keys.TAB).pause(1).send_keys(Keys.TAB).perform()
    13. # 组合键操作 ctrl+a、ctrl+c、ctrl+v、shift+a
    14. # 修饰键 ctrl、alt、shift
    15. # ctrl+a ==> 按下ctrl、按下a、松开a、松开ctrl
    16. driver.find_element(By.XPATH,'//input[@id="kw"]').send_keys('python')
    17. ActionChains(driver).key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()
    18. # 备注:系统级别的组合键不能使用,因为这里的组合键都是只针对网页生效的

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

    软件测试面试小程序

    被百万人刷爆的软件测试题库!!!谁用谁知道!!!全网最全面试刷题小程序,手机就可以刷题,地铁上公交上,卷起来!

    涵盖以下这些面试题板块:

    1、软件测试基础理论 ,2、web,app,接口功能测试 ,3、网络 ,4、数据库 ,5、linux

    6、web,app,接口自动化 ,7、性能测试 ,8、编程基础,9、hr面试题 ,10、开放性测试题,11、安全测试,12、计算机基础

    资料获取方式 :

  • 相关阅读:
    Abnova LiquidCell-负富集细胞分离和回收系统
    01 python编码语法
    全媒体时代,企业新闻通稿应该怎么写更具传播性
    yolov8 snpe报错 Invalid Slice inputs 4, only constant inputs supported
    我看见了黑洞
    浅谈非线性回归(non-linear regression)
    Linux进阶-用户管理与文件权限
    告别手机自带浏览器,分享2022年好用的手机浏览器
    1.验证码绕过
    浅学枚举类
  • 原文地址:https://blog.csdn.net/m0_58026506/article/details/132695232