UI自动化测试 鼠标键盘的一些操作
from selenium import webdriver import time from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys driver = webdriver.Chrome() driver.get('file:///F:/p9%E7%AC%94%E8%AE%B0/UI%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95/%E6%B5%8B%E8%AF%95%E4%BB%A3%E7%A0%81/pagetest/%E6%B3%A8%E5%86%8CA.html') ''' 鼠标操作的步骤 1.生成鼠标对象:ActionChains 2.做对应的操作:context_click,double_click, move_to_element,click 3.执行相应的操作:perform ''' # 生成鼠标操作对象 # action = ActionChains(driver) # # # 调用鼠标右击方法 # action.context_click(driver.find_element(By.ID, "userA")) # # # 输入用户名,双击用户 # element = driver.find_element(By.ID, "userA") # element.send_keys('admin') # time.sleep(3) # # # 调用鼠标双击事件方法 # action.double_click(element) # # # 悬停在注册上边 # element = driver.find_element(By.CSS_SELECTOR, "button") # # 调用鼠标悬停事件方法 # action.move_to_element(element) # # 调用鼠标执行的方法 # action.perform() # 生成键盘操作对象 ''' 1.send_keys(Keys.BACK_SPACE) 删除键(BackSpace) 2.send_keys(Keys.SPACE) 空格键(Space) 3.send_keys(Keys.TAB) 制表键(Tab) 4.send_keys(Keys.ESCAPE) 回退键(Esc) 5.send_keys(Keys.ENTER) 回车键(Enter) 6.send_keys(Keys.CONTROL,'a') 全选(Ctrl+A) 7.send_keys(Keys.CONTROL,'c') 复制(Ctrl+C) 8.send_keys(Keys.CONTROL,'v') 粘贴 ''' # 1).输入用户名:admin1,暂停2秒,删除1 element = driver.find_element(By.ID, 'userA') element.send_keys('admin1') time.sleep(2) element.send_keys(Keys.BACK_SPACE) # 删除最后一个字符串 clear() # 2).全选用户名:admin,暂停2秒 element.send_keys(Keys.CONTROL, 'a') time.sleep(2) # 3).复制用户名:admin,暂停2秒 b = element.send_keys(Keys.CONTROL, 'c') time.sleep(2) # 4). 粘贴到密码框 driver.find_element(By.ID, 'passwordA').send_keys(Keys.CONTROL, 'v')