Keys 类键盘操作的常用方法

 

Keys 类键盘操作的常用方法:   

send_keys(Keys.BACK_SPACE) 删除键(BackSpace)   

send_keys(Keys.SPACE) 空格键(Space)   

send_keys(Keys.TAB) 制表键(Tab)   

send_keys(Keys.ESCAPE) 回退键(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)

导入模块:from selenium.webdriver.common.keys import Keys

#-*-coding:utf-8-*-
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome()
driver.get('http://www.baidu.com')
driver.maximize_window()
time.sleep(3)
driver.find_element_by_link_text('hao123').send_keys(Keys.ENTER)
time.sleep(5)
print(driver.current_url)#打印当前页面URL
driver.back()
time.sleep(3)
menu = driver.find_element_by_link_text('设置')
ActionChains(driver).move_to_element(menu).perform()
driver.find_element_by_link_text('搜索设置').click()
time.sleep(3)
driver.find_element_by_id('nr').click()
time.sleep(3)
driver.find_element_by_id('nr').send_keys(Keys.ESCAPE)#回退键
time.sleep(3)
driver.find_element_by_css_selector("span[title='关闭']").click()
time.sleep(3)
driver.find_element_by_id('kw').send_keys('腾讯NBAB')
time.sleep(3)
driver.find_element_by_id('kw').send_keys(Keys.BACK_SPACE)#删除键
time.sleep(3)
driver.find_element_by_id('kw').send_keys(Keys.SPACE)#空格键
time.sleep(3)
driver.find_element_by_id('kw').clear()
time.sleep(3)
driver.find_element_by_id('kw').send_keys('腾讯NBA')
time.sleep(3)
driver.find_element_by_id('kw').send_keys(Keys.CONTROL,'a')#全选,Ctrl+A
time.sleep(3)
driver.find_element_by_id('kw').send_keys(Keys.CONTROL,'x')#剪切,Ctrl+X
time.sleep(3)
driver.find_element_by_id('kw').send_keys(Keys.CONTROL,'v')#粘贴,Ctrl+V
time.sleep(3)
driver.find_element_by_id('su').send_keys(Keys.ENTER)#回车键
time.sleep(5)
driver.quit()

 

posted @ 2019-05-07 21:43  Avicii_2018  阅读(2130)  评论(0编辑  收藏  举报