Selenium-webdriver (Python) (12) Keyboard key combination usage

Source: Internet
Author: User
Tags sleep

This section focuses on:

L Keyboard Key Usage

L Keyboard key combination usage

L Send_keys () input Chinese run error problem

Keyboard Key Usage:

#coding =utf-8 from
Selenium import webdriver The
import keys from Selenium.webdriver.common.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 () # Browser full screen
    
driver.find_element_by_id ("user_name"). Clear ()
driver.find_element_by_id ("user_name"). Send_keys ("Fnngj")
    
#tab的定位相相于清除了密码框的默认提示信息, equal to the clear above ()
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 (enter) instead of landing button
driver.find_element_by_id ("User_pwd" ). Send_keys (keys.enter)
'
#也可定位登陆按钮, replace click ()
driver.find_element_by_id ("Login") by enter (enter). Send_keys (keys.enter)
'
time.sleep (3) Time.sleep (
    
3)
driver.quit ()

To invoke the keyboard key operation, you need to introduce the keys package:

From Selenium.webdriver.common.keys import keys

To invoke the key by Send_keys ():

Send_keys (keys.tab) # TAB

Send_keys (keys.enter) # Enter

Note: This operation and page elements of the traversal order, if the current positioning in the Account input box, press the Keyboard tab after the traversal is not a password box, it is illegal to enter a password. If you enter the password, there is a need to fill in the verification code, then enter the landing will not be the effect.

This is not a good method, just to verify the use of a single key

Keyboard combination Key Usage:

#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 full selection of input box content
driver.find_element_by_id (" kw "). Send_keys (Keys.control, ' A ')
Time.sleep (3)
    
#ctrl +x cut input box content
driver.find_element_by_id ("kw"). Send_keys (Keys.control, ' x ')
Time.sleep (3)
    
#输入框重新输入内容, search
driver.find_element_by_id ("kw"). Send_keys (U "Pest cnblogs")
driver.find_element_by_id ("su"). Click ()
    
time.sleep (3)
driver.quit ()

The above operation has no practical significance, but shows us the use of keyboard combination keys.

Enter Chinese question

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

By the way, I solved a problem with an input file that bothered me to present. Selenium2 python in Send_keys () in Chinese has been the error, in fact, add a small u in front of the solution:

Send_keys (U "enter Chinese")

Basically all the buttons on the keyboard can simulate

Module:selenium.webdriver.common.keys class keys () –null = U ' ue000 ' –cancel = U ' ue001 ' ^break–help = U ' ue002 ' –ba ck_space = U ' ue003 ' –tab = U ' ue004 ' –clear = U ' ue005 ' –return = U ' ue006 ' –enter = U ' ue007 ' –shift = U ' ue008 ' –left_s Hift = U ' ue008 ' # alias–control = U ' ue009 ' –left_control = U ' ue009 ' # alias–alt = U ' ue00a ' –left_alt = U ' ue00a ' # al 
Ias–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 ' –delet E = U ' ue017 ' –semicolon = U ' ue018 ' –equals = U ' ue019 ' –numpad0 = U ' ue01a ' # numbe pad keys–numpad1 = U ' ue01b ' –numpa D2 = U ' ue01c ' –numpad3 = U ' ue01d ' –numpad4 = U ' ue01e ' –numpad5 = U ' ue01f ' –numpad6 = U ' ue020 ' –numpad7 = U ' ue021 ' –n UMPAD8 = 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 ' ue03 d ' –command = U ' ue03d '
Related Article

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.