1. 程式人生 > >python+selenium中快捷鍵的操作

python+selenium中快捷鍵的操作

Keys()類提供了快捷鍵的方法

經常是用到的快捷鍵操作

send_keys(Keys.BACKSPACE)  刪除鍵

send_keys(keys.ENTER)            回車鍵

send.keys(Keys.SPACE)            空格鍵

send.keys(Keys.CONTROL,'a')  快捷鍵control+A

send.keys(Keys.CONTROL,'c')  快捷鍵control+C

#coding=utf-8
 
from selenium import webdriver
#匯入Keys()類
from selenium.webdriver.common.keys import Keys
 
driver = webdriver.Firefox()
driver.get('https://www.baidu.com')
 
driver.find_element_by_id('kw').send_keys('seleniumm')
#刪除多餘的一個m
driver.find_element_by_id('kw').send_keys(Keys.BACK_SPACE)
#輸入空格鍵+教程
driver.find_element_by_id('kw').send_keys(Keys.SPACE)
driver.find_element_by_id('kw').send_keys(u'教程')
#全選輸入框內容,使用control+A
driver.find_element_by_id('kw').send_keys(Keys.CONTROL,'a')
#剪下輸入框內容
driver.find_element_by_id('kw').send_keys(Keys.CONTROL,'x')
#貼上在文字框中
driver.find_element_by_id('kw').send_keys(Keys.CONTROL,'v')
#回車搜尋
driver.find_element_by_id('kw').send_keys(Keys.ENTER)
 
driver.quit()