輕鬆自動化---selenium-webdriver(python) (十二)

蟲師發表於2013-08-15

本節重點:

  • l 鍵盤按鍵用法
  • l 鍵盤組合鍵用法
  • l send_keys() 輸入中文執行報錯問題

 

 

鍵盤按鍵鍵用法:

#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys  #需要引入keys包
import os,time

driver = webdriver.Firefox()
driver.get("http://passport.kuaibo.com/login/?referrer=http%3A%2F%2Fwebcloud.kuaibo.com%2F")

time.sleep(3)
driver.maximize_window() # 瀏覽器全屏顯示

driver.find_element_by_id("user_name").clear()
driver.find_element_by_id("user_name").send_keys("fnngj")

#tab的定位相相於清除了密碼框的預設提示資訊,等同上面的clear()
driver.find_element_by_id("user_name").send_keys(Keys.TAB)
time.sleep(3)
driver.find_element_by_id("user_pwd").send_keys("123456")

#通過定位密碼框,enter(回車)來代替登陸按鈕
driver.find_element_by_id("user_pwd").send_keys(Keys.ENTER)
'''
#也可定位登陸按鈕,通過enter(回車)代替click()
driver.find_element_by_id("login").send_keys(Keys.ENTER)
'''
time.sleep(3)

time.sleep(3)
driver.quit()

要想呼叫鍵盤按鍵操作需要引入keys包:

from selenium.webdriver.common.keys import Keys

通過send_keys()呼叫按鍵:

send_keys(Keys.TAB)        # TAB

send_keys(Keys.ENTER)    # 回車

 

注意:這個操作和頁面元素的遍歷順序有關,假如當前定位在賬號輸入框,按鍵盤的tab鍵後遍歷的不是密碼框,那就不法輸入密碼。 假如輸入密碼後,還有需要填寫驗證碼,那麼回車也起不到登陸的效果。

這並不是一種好的方法,這裡只是為了驗證單個按鍵的用法

 

 

鍵盤組合鍵用法:

#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os,time

driver = webdriver.Firefox()

driver.get("http://www.baidu.com")

#輸入框輸入內容
driver.find_element_by_id("kw").send_keys("selenium")
time.sleep(3)

#ctrl+a 全選輸入框內容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a')
time.sleep(3)

#ctrl+x 剪下輸入框內容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'x')
time.sleep(3)

#輸入框重新輸入內容,搜尋
driver.find_element_by_id("kw").send_keys(u"蟲師 cnblogs")
driver.find_element_by_id("su").click()

time.sleep(3)
driver.quit()

上面的操作沒有實際意義,但向我們演示了鍵盤組合按鍵的用法。

 

 

輸入中文問題

順便解決了一個困擾我到現的一個輸入中檔案的問題。selenium2 pythonsend_keys()中輸入中文一直報錯,其實前面加個小就解決了:

send_keys(u"輸入中文")

 

 

基本上鍵盤上所有的按鍵都可以模擬

module:selenium.webdriver.common.keys
• class Keys()
– NULL = u’ue000’
– CANCEL = u’ue001’ # ^break
– HELP = u’ue002’
– BACK_SPACE = u’ue003’
– TAB = u’ue004’
– CLEAR = u’ue005’
– RETURN = u’ue006’
– ENTER = u’ue007’
– SHIFT = u’ue008’
– LEFT_SHIFT = u’ue008’ # alias
– CONTROL = u’ue009’
– LEFT_CONTROL = u’ue009’ # alias
– ALT = u’ue00a’
– LEFT_ALT = u’ue00a’ # alias
– PAUSE = u’ue00b’
– ESCAPE = u’ue00c’
– SPACE = u’ue00d’
– PAGE_UP = u’ue00e’
– PAGE_DOWN = u’ue00f’
– END = u’ue010’
– HOME = u’ue011’
– LEFT = u’ue012’
– ARROW_LEFT = u’ue012’ # alias
– UP = u’ue013’
– ARROW_UP = u’ue013’ # alias
– RIGHT = u’ue014’
– ARROW_RIGHT = u’ue014’ # alias
– DOWN = u’ue015’
– ARROW_DOWN = u’ue015’ # alias
– INSERT = u’ue016’
– DELETE = u’ue017’
– SEMICOLON = u’ue018’
– EQUALS = u’ue019’
– NUMPAD0 = u’ue01a’ # numbe pad keys
– NUMPAD1 = u’ue01b’
– NUMPAD2 = u’ue01c’
– NUMPAD3 = u’ue01d’
– NUMPAD4 = u’ue01e’
– NUMPAD5 = u’ue01f’
– NUMPAD6 = u’ue020’
– NUMPAD7 = u’ue021’
– NUMPAD8 = u’ue022’
– NUMPAD9 = u’ue023’
– MULTIPLY = u’ue024’
– ADD = u’ue025’
– SEPARATOR = u’ue026’
– SUBTRACT = u’ue027’
– DECIMAL = u’ue028’
– DIVIDE = u’ue029’
– F1 = u’ue031’ # function keys
– F2 = u’ue032’
– F3 = u’ue033’
– F4 = u’ue034’
– F5 = u’ue035’
– F6 = u’ue036’
– F7 = u’ue037’
– F8 = u’ue038’
– F9 = u’ue039’
– F10 = u’ue03a’
– F11 = u’ue03b’
– F12 = u’ue03c’
– META = u’ue03d’
– COMMAND = u’ue03d’

 

 

 

--------------------------

學習更多selenium 內容:

 「功能測試自動化」彙總

 

相關文章