码迷,mamicode.com
首页 > 移动开发 > 详细

appium 键盘按键用法

时间:2017-07-06 14:19:04      阅读:425      评论:0      收藏:0      [点我收藏+]

标签:账号   class   组合键   注意   nbsp   div   没有   code   ace   

  • 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 python在send_keys()中输入中文一直报错,其实前面加个小u 就解决了:

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’

appium 键盘按键用法

标签:账号   class   组合键   注意   nbsp   div   没有   code   ace   

原文地址:http://www.cnblogs.com/jiuyigirl/p/7125723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!