UI自动化测试Keys类实战
在UI的自动化测试中,会遇到键盘事件的操作,使用到的类为Keys,导入方式如下:
from selenium.webdriver.common.keys import Keys
回车
from selenium import webdriver from selenium.webdriver.common.keys import Keys import time as t '''键盘事件''' # 回车 driver=webdriver.Chrome() driver.maximize_window() driver.get('http://www.baidu.com') so=driver.find_element_by_id('kw') t.sleep(3) so.send_keys('五岳') t.sleep(3) so.send_keys(Keys.ENTER) t.sleep(5) driver.quit()
键盘快捷键
from selenium import webdriver from selenium.webdriver.common.keys import Keys import time as t '''键盘事件''' # 回车 driver=webdriver.Chrome() driver.maximize_window() driver.get('http://www.baidu.com') so=driver.find_element_by_id('kw') t.sleep(3) so.send_keys('五岳') t.sleep(3) # 全选 so.send_keys(Keys.CONTROL,'a') t.sleep(3) # 复制 so.send_keys(Keys.CONTROL,'c') t.sleep(3) # 全部删除 so.send_keys(Keys.BACKSPACE) t.sleep(3) # 粘贴 so.send_keys(Keys.CONTROL,'v') t.sleep(3) # 回车 so.send_keys(Keys.ENTER) t.sleep(5) driver.quit()
浏览器的滑动
from selenium import webdriver from selenium.webdriver.common.keys import Keys import time as t driver=webdriver.Chrome() driver.maximize_window() driver.get('http://hao123.com') driver.find_element_by_name('word') driver.implicitly_wait(30) t.sleep(3) # 向下滑动 down="var q=document.documentElement.scrollTop=10000" driver.execute_script(down) t.sleep(5) #向上滑动 up="var q=document.documentElement.scrollTop=0" t.sleep(5) driver.execute_script(up) driver.quit()