自动化测试——selenium学习日记3(操作鼠标)

本文详细介绍了如何使用Python的Selenium库进行自动化测试,包括click(),context_click(),double_click(),drag_and_drop()等鼠标操作方法,以及元素定位和坐标移动技巧。
摘要由CSDN通过智能技术生成

鼠标点击在测试中式必不可少的,自动化测试鼠标点击需要先导入:from selenium.webdriver import ActionChains

鼠标点击的方式有:

鼠标点击 :click()

鼠标右击: context_click(elem).perform()

双击:double_click(elem).perform()

长按右键:click_and_hold(elem).perform()

移动鼠标从一个元素到另一个元素:drag_and_drop(elem_a, elem_b).perform()

拖拽元素到指定位置:drag_and_drop_by_offset(elem, x ,y)

鼠标移动到指定元素:move_to_element(elem).perform()

鼠标移动到距离指定元素多少的位置:move_to_element_with_offeset(elem, x, y).perform()

在指定元素位置松开鼠标:click_and_hold(elem).release(elem).perform()

#conding=utf-8
from selenium import webdriver
import time
from selenium.webdriver import ActionChains  # 鼠标操作
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://www.baidu.com')

# 实例化鼠标
acti = ActionChains(driver)

# 鼠标点击
driver.find_element(By.ID,'kw').click()

# 长按鼠标左键
elem = driver.find_element(By.LINK_TEXT,'hao123')
acti.click_and_hold(elem).perform()
time.sleep(3)

# 鼠标右击
acti.context_click(elem).perform()
time.sleep(3)

# 双击
elem_a = driver.find_element(By.XPATH,'//*[@id="kw"]')
acti.double_click(elem_a).perform()
time.sleep(3)

# 拖动元素到另一个元素处elem->elem_a
acti.drag_and_drop(elem, elem_a).perform()
time.sleep(3)

#拖拽元素到指定位置
acti.drag_and_drop_by_offset(elem_a, 100, 100).perform()
time.sleep(3)

# 鼠标移动到指定元素
elem_b = driver.find_element(By.LINK_TEXT,'更多')
acti.move_to_element(elem_b).perform()
time.sleep(3)

# 鼠标移动到指定坐标位置
acti.move_by_offset(200, 200).perform()
time.sleep(3)

#鼠标移动到距离元素多少的位置
acti.move_to_element_with_offset(elem, 300, 300).perform()
time.sleep(3)

# 在指定元素位置松开鼠标
elem_c = driver.find_element(By.CSS_SELECTOR,'#s-top-left > a:nth-child(1)')
acti.click_and_hold(elem_c).perform()
acti.click_and_hold(elem_a).release(elem_a).perform()
time.sleep(3)

driver.quit()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值