python_selenium_键盘事件
引言
----在实际的web测试工作中,需要配合键盘按键来操作,webdriver的 keys()类提供键盘上所有按键的操作,还可以模拟组合键Ctrl+a,Ctrl+v等。
举例:
#cording=gbk
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By #导入by方法
from selenium.webdriver.common.action_chains import ActionChains ##对鼠标事件操作
from selenium.webdriver.common.keys import Keys # 对键盘事件操作
current_path=os.path.dirname(__file__)
firefox_path=current_path+"/../webdriver/geckodriver.exe"
driver=webdriver.Firefox(executable_path=firefox_path)
driver.get("http://www.baidu.com")
# 先输入百度
driver.find_element_by_id('kw').send_keys('百度')
time.sleep(3)
# 1.删除度
driver.find_element_by_id('kw').send_keys(Keys.BACK_SPACE)
time.sleep(3)
#2.清空输入框,重新输入值
driver.find_element_by_id('kw').clear()
driver.find_element_by_id('kw').send_keys('安琪儿')
time.sleep(5)
# 3.ctrl+a 全选输入框里的内容
driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'a')
time.sleep(3)
# 4.ctrl+x 剪切输入框里的内容
driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'x')
time.sleep(3)
# 5. ctrl+v 粘贴剪切的内容
driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'v')
time.sleep(3)
# 6. 回车
driver.find_element_by_id('su').send_keys(Keys.ENTER)
time.sleep(3)