(Xvi) WebDriver API's key events

Essays and records to facilitate their access to fellow travelers.

#------------------------------------------------I ------------------------------------------- dividing line is a shame

  Learning before selenium automation, it is best to learn HTML, CSS, JavaScript and other knowledge, help to understand the principles of operation and positioning elements. About python and selenium install their own search for other information,

Here do not introduced, all examples use python3.6 + selenium execution.

#------------------------------------------------I ------------------------------------------- dividing line is a shame

Keyboard Events

  Keys () class provides almost all of the keys on the keyboard. See the article mentioned send_keys () method can be used to simulate keyboard input, in addition, we can also use it to enter keys on the keyboard, or even a combination of keys, such as the Ctrl + A , the Ctrl + C and the like.

 

from selenium import webdriver
#导入keys模块
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get('http://www.baidu.com')

# Input box content 
driver.find_element_by_id ( ' kW ' ) .send_keys ( ' seleniumm ' )

# Delete a multi-input m 
driver.find_element_by_id ( ' kW ' ) .send_keys (Keys.BACK_SPACE)

# Input Space + "Tutorial" 
driver.find_element_by_id ( ' kW ' ) .send_keys (Keys.SPACE)
driver.find_element_by_id('kw').send_keys("教程")

# Ctrl + A select all content input box 
driver.find_element_by_id ( ' kW ' ) .send_keys (Keys.CONTROL, ' A ' )

# Ctrl + X shear content input box 
driver.find_element_by_id ( ' kW ' ) .send_keys (Keys.CONTROL, ' X ' )


# Ctrl + V to paste the contents of the input block 
driver.find_element_by_id ( ' kW ' ) .send_keys (Keys.CONTROL, ' V ' )

# Instead of the operation by clicking the Enter key 
driver.find_element_by_id ( ' SU ' ) .send_keys (Keys.ENTER)

 

  It should be noted that the above script of little practical significance, only to show us the use of a variety of simulated keyboard keys and combinations.

  from selenium.webdriver.common.keys import Keys

  Before using the method needs to be imported keyboard keys class.

  The following is a commonly used keyboard:

  send_keys (Keys.BACK_SPACE)   Delete key (AackSpace)

  send_keys (Keys.SPACE)   spacebar (Space)

  send_keys (Keys.TAB)   tab (Tab)

  send_keys (Keys.ESCAPE)   Backspace key (Ese)

  send_keys (Keys.ENTER)   Enter key (Enter)

  send_keys(Keys.CONTROL,'a')  全选(ctrl+a)

  send_keys(Keys.CONTROL,'c')  复制(ctrl+c)

  send_keys (Keys.CONTROL, 'x')   Cut (ctrl + x)

  send_keys (Keys.CONTROL, 'v')   paste (ctrl + v)

  send_keys (Keys.F1)   Keyboard F1

  ...

  send_keys (Keys.F12)   keyboard F12

 

Guess you like

Origin www.cnblogs.com/lirongyang/p/11457781.html