Selenium Study Note 014: Keyboard usage

Source: Internet
Author: User

Learn the main points of knowledge:

Keyboard Key Usage

Keyboard combination Key Usage

Send_keys () Input Chinese operation error problem


First, keyboard key usage:

#coding =utf-8from Selenium Import webdriverfrom selenium.webdriver.common.keys import keys  #需要引入keys包import OS, Timedriver = Webdriver. Firefox () driver.get ("Http://passport.kuaibo.com/login/?referrer=http%3A%2F%2Fwebcloud.kuaibo.com%2F") Time.sleep (3) Driver.maximize_window () # Browser Full screen display driver.find_element_by_id ("user_name"). Clear () driver.find_element _BY_ID ("user_name"). Send_keys ("Fnngj") #tab的定位相相于清除了密码框的默认提示信息, equivalent to the upper clear () driver.find_element_by_id ("user_name "). Send_keys (Keys.tab) #TAB键time. Sleep (3) driver.find_element_by_id (" User_pwd "). Send_keys (" 123456 ") #通过定位密码框, Enter (enter) to replace the login button driver.find_element_by_id ("User_pwd"). Send_keys (Keys.enter) #ENTER键 "#也可定位登陆按钮, Replace the Click () driver.find_element_by_id ("login") with the Enter (enter). Send_keys (Keys.enter) "Time.sleep (3) time.sleep (3) Driver.quit ()

Note : This operation and page elements of the traversal order, if the current location in the Account input box, press the Keyboard tab key after the traversal is not the password box, then enter the password illegally. If you enter a password, there is a need to fill in the verification code, then the return will not be the effect of landing. This is not a good approach, just to verify the use of individual keys


Two, keyboard key combinations usage:

#coding =utf-8from Selenium Import webdriverfrom selenium.webdriver.common.keys import Keysimport os,timedriver = Webdriver. Firefox () driver.get ("http://www.baidu.com") #输入框输入内容driver. find_element_by_id ("kw"). Send_keys ("Selenium") Time.sleep (3) #ctrl +a Select all input box contents driver.find_element_by_id ("kw"). Send_keys (Keys.control, ' a ') Time.sleep (3) #ctrl +x Cut the input box contents driver.find_element_by_id ("kw"). Send_keys (Keys.control, ' x ') Time.sleep (3) #输入框重新输入内容, search Driver.find_ element_by_id ("kw"). Send_keys (U "Insect Division Cnblogs") driver.find_element_by_id ("su"). Click () time.sleep (3) driver.quit ()


Iii. Importing Chinese questions

By the way solved a problem that bothered me with an input file in the present. selenium2 python in send_keys () input Chinese has been an error, in fact, a small u before the solution:

Send_keys (U "input Chinese")


basically all keys on the keyboard can be simulated, with analog keyboard operation code:

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 ' UE 00d ' –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 ' –arro W_down = U ' ue015 ' # alias–insert = U ' ue016 ' –delete = U ' ue017 ' –semicolon = U ' ue018 ' –equals = U ' ue019 ' –numpad0 = U ' ue01 A ' # Numbe pad keys–numpad1 = U ' ue01b ' –numpad2 = U ' ue01c ' –numpad3 = U ' ue01d ' –numpad4 = U ' ue01e ' –numpad5 = U ' ue01f ' –n UMPAD6 = 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 ' –f1 0 = U ' ue03a ' –f11 = U ' ue03b ' –f12 = U ' ue03c ' –meta = U ' ue03d ' –command = U ' ue03d '
This document is part of the reference to the pest master Webdriver.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Selenium Study Note 014: Keyboard usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.