Python selenium-webdriver element Operation keyboard operation

Source: Internet
Author: User
Tags delete key

Selenium provides a more complete keyboard operation that requires us to import before using the analog keyboard operation from selenium.webdriver.common.keys import Keys , and then you can simulate the keyboard operation.

# Import the Keys module, and then we'll look at the keys module that defines the keys  from Import Keys

After importing the module, we can look at the specific definition of the key content, I have been frequently used to mark the key notes.

    NULL = ' \ue000 ' CANCEL = ' \ue001 ' # ^break help = ' \ue002 ' BACKSPACE = ' \ue003 ' back_space = BACKSPACE   #删除键 TAB = ' \ue004 ' #TAB键 CLEAR = ' \ue005 ' RETURN = ' \ue006 ' ENTER = ' \ue007 ' #回车键 SHIFT = ' \ue008 ' #Shift键 Left_shift = SHIFT control = ' \ue009 ' Left_control = control #Ctrl Key ALT = ' \ue00a ' #Alt   Key Left_alt = ALT PAUSE = ' \ue00b ' ESCAPE = ' \ue00c ' #ECS键 SPACE = ' \ue00d ' #空格键 page_up = ' \ue00e '     #PgUp key Page_down = ' \ue00f ' #PgDwon key END = ' \ue010 ' #END key HOME = ' \ue011 ' #HOME key left = ' \ue012 ' #左键 Arrow_left = left up = ' \ue013 ' #上键 arrow_up = "up" = ' \ue014 ' arrow_right = right #右键 DOW N = ' \ue015 ' #下键 arrow_down = down INSERT = ' \ue016 ' #insert键 DELETE = ' \ue017 ' #del键 semicolon = ' \ue018 ' # '; ' Key EQUALS = ' \ue019 ' # ' = ' key #数字键盘 NUMPAD0 = ' \ue01a ' # number pad keys NUMPAD1 = ' \ue01b ' NUMPAD2 = ' \ue01 C ' NUMPAD3 = ' \ue01d ' NUMPAD4 = ' \ue01e ' NUMPAD5 = ' \ue01f ' NUMPAD6 = ' \ue020 ' NUMPAD7 = ' \ue021 ' NUMPAD8 = ' \ue 022 ' NUMPAD9 = ' \ue023 ' MULTIPLY = ' \ue024 ' # ' * ' key ADD = ' \ue025 ' # ' + ' key SEPARATOR = ' \ue026 ' # ', ' key SU btract = ' \ue027 ' # '-' key DECIMAL = ' \ue028 ' # '. ' Key DIVIDE = ' \ue029 ' # '/' key F1 = ' \ue031 ' # function keys F2 = ' \ue032 ' F3 = ' \ue033 ' F4 = ' \ue034 ' F 5 = ' \ue035 ' F6 = ' \ue036 ' F7 = ' \ue037 ' F8 = ' \ue038 ' F9 = ' \ue039 ' F10 = ' \ue03a ' F11 = ' \ue03b ' F1 2 = ' \ue03c ' META = ' \ue03d ' COMMAND = ' \ue03d '

See the module contains a lot of keys but the actual use of the keys are not many, so I face the key to the operation of a number of ways to introduce, will not be all the keys detailed introduction.

1. First we understand the key combination, what is a combination of keys, such as we often use the CTRL + A, CTRL + C and so on are key combinations. Let's look at an example first.

When using the key operation we need to use Send_keys () to simulate the operation, Keys.control is the CTRL key on our keyboard, here are a few common key combinations.

Send_keys (Keys.control, ' a ') #全选 (Ctrl + a)

Send_keys (Keys.control, ' C ') #复制 (ctrl + D)

Send_keys (Keys.control, ' x ') #剪切 (ctrl+x)

Send_keys (Keys.control, ' V ') #粘贴 (Ctrl + v.)

Below we use CTRL + A to write an instance to see the use of key combinations.

#-*-coding:utf-8-*- fromSelenium.webdriver.common.keysImportKeys fromSeleniumImportWebdriverImportTimedriver=Webdriver. Chrome () Driver.get ("http://www.baidu.com") driver.find_element_by_id ('kw'). Send_keys ('aaaaaaaaaaaa') driver.find_element_by_id ("kw"). Send_keys (Keys.control,'a')#Note the input of the key combinations here. Time.sleep (10) Driver.quit ()

2. The following commonly used keys, these common keys are mainly non-combination keys, directly input can.

Enter Keys.enter
Delete key Keys.back_space
Space bar Keys.space
TAB key Keys.tab
Fallback key Keys.escape
Refresh Key Keys.f5

Here we choose a common key, to write an example, use the ENTER key instead of the mouse click ()

#-*-coding:utf-8-*- fromSelenium.webdriver.common.keysImportKeys fromSeleniumImportWebdriverImportTimedriver=Webdriver. Chrome () Driver.maximize_window () Driver.get ("http://cn.bing.com/") driver.find_element_by_id ('sb_form_q'). Send_keys ('Selenium') driver.find_element_by_id ("Sb_form_go"). Send_keys (Keys.enter)#Replace the left mouse button with the Enter keyDriver.quit ()

Here we can see that the simulation effect is the same as the mouse.

 

The above two examples illustrate the use of a combination of keys and a single button to manipulate the elements, because the keys are relatively simple to use, mainly to recognize the meaning of each key, so that we can use the button smoothly

Python selenium-webdriver element Operation keyboard operation

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.