文章目录
- 一、鼠标操作
- 二、键盘操作
一、鼠标操作
1、在web测试中,鼠标的操作包含在ActionChains类中,经常用到的有单击、双击、右击、拖动等操作。
2、在使用鼠标操作前需要先导入ActionChains类包:
from selenium.webdriver.common.action_chains import ActionChains
3、常用操作
click(ele) | 鼠标左键单击 |
context_click(ele) | 鼠标右键单击 |
double_click(ele) | 鼠标左键双击 |
drag_and_drop(source, target) | 拖动到某个元素后松开 |
move_to_element(ele) | 鼠标悬停在一个元素上 |
click_and_hold(ele) | 鼠标左键单击,不松开 |
release() | 在某个元素上松开鼠标左键 |
perform() | 执行上述鼠标操作 |
4、实操
①左击操作:click(ele)
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# 定位到搜索栏,输入"selenium"
driver.find_element(By.ID, "kw").send_keys("selenium")
# 定位到百度一下按钮
ele = driver.find_element(By.ID, "su")
# 实例化ActionChains类
action = ActionChains(driver)
# 调用click()点击操作
click_ele = action.click(ele)
# 点击后必须调用perform()执行
click_ele.perform()
time.sleep(3)
driver.quit()
②右击操作:context_click(ele)
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# 定位到百度一下按钮
ele = driver.find_element(By.ID, "su")
# 实例化ActionChains类
action = ActionChains(driver)
# 调用contex_click()点击右键操作
right_click_ele = action.context_click(ele)
# 点击后必须调用perform()执行
right_click_ele.perform()
time.sleep(3)
driver.quit()
③双击操作:double_click(ele)
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# 找到搜索栏元素
search = driver.find_element(By.ID, "kw")
# 输入"selenium"
search.send_keys("selenium")
time.sleep(3)
# 实例化ActionChains类
action = ActionChains(driver)
# 调用double_click()双击操作输入框
double_ele = action.double_click(search)
# 点击后必须调用perform()执行
double_ele.perform()
time.sleep(3)
driver.quit()
④拖动操作:drag_and_drop(ele1, ele2)
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# 找到百度热搜元素
hot_search = driver.find_element(By.CLASS_NAME, "hot-title")
# 找到百度搜索栏输入框
search = driver.find_element(By.ID, "kw")
# 实例化ActionChains类
action = ActionChains(driver)
# 调用drag_and_drop()按住第一个元素,拖动到第二个元素松开,并调用perform()执行
action.drag_and_drop(hot_search, search).perform()
time.sleep(3)
driver.quit()
⑤悬停操作:move_to_element(ele)
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# 找到百度热搜元素
hot_search = driver.find_element(By.CLASS_NAME, "hot-title")
time.sleep(3)
# 实例化ActionChains类
action = ActionChains(driver)
# 鼠标悬停在百度热搜元素上,并执行
action.move_to_element(hot_search).perform()
time.sleep(3)
driver.quit()
⑥按下左键操作:click_and_hold(ele)
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# 找到百度热搜元素
hot_search = driver.find_element(By.CLASS_NAME, "hot-title")
time.sleep(3)
# 实例化ActionChains类
action = ActionChains(driver)
# 在百度热搜元素上按下鼠标左键不松开
action.click_and_hold(hot_search).perform()
time.sleep(3)
driver.quit()
二、键盘操作
1、在web测试中,键盘操作我们常有的如删除、空格、回车、复制、粘贴等。而这些操作都包含在Keys类中。
2、在使用键盘操作前需要先导入Keys类包:
from selenium.webdriver.common.keys import Keys
3、常用操作
send_keys(Keys.BACK_SPACE) | 删除键(BackSpace) |
send_keys(Keys.SPACE) | 空格键(Space) |
send_keys(Keys.TAB) | 制表键(Tab) |
send_keys(Keys.COMMAND) | 回退(Esc) |
send_keys(Keys.ENTER) | 回车(Enter) |
send_keys(Keys.CONTROL,‘a’) | 全选(Ctrl+A) |
send_keys(Keys.CONTROL,‘c’) | 复制(Ctrl+C) |
send_keys(Keys.CONTROL,‘x’) | 剪切(Ctrl+X) |
send_keys(Keys.CONTROL,‘v’) | 粘贴(Ctrl+V) |
send_keys(Keys.F1) | 键盘F1 |
send_keys(Keys.F5) | 键盘F5 |
send_keys(Keys.F12) | 键盘F12 |
注意:mac系统要将CONTROL键替换成COMMAND键!
4、实操
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.maximize_window()
# 定位搜索输入栏元素
search = driver.find_element(By.ID, "kw")
# 输入"sleenium"
search.send_keys("selenium")
time.sleep(3)
# 删除最后一个字母
search.send_keys(Keys.BACK_SPACE)
time.sleep(3)
# 全选
search.send_keys(Keys.COMMAND, "a")
time.sleep(3)
# 复制
search.send_keys(Keys.COMMAND, "c")
time.sleep(3)
# 输入一个空格
search.send_keys(Keys.SPACE)
time.sleep(3)
# 粘贴
search.send_keys(Keys.COMMAND, "v")
time.sleep(3)
driver.quit()
注意:我这里用的是mac系统,用的是COMMAND键,Windows系统应该是CONTROL键加相应操作。