Usage of common WebDriver API ---- 7

1: a single key operation of the keyboard analog

from the Selenium Import webdriver
 from selenium.webdriver.common.keys Import   Keys
 Import Time 
Driver = webdriver.Chrome ()
 # access custom URL 
driver.get ( " http://www.sogou.com " )
 # by id targeted to search input box page elements 
Query = driver.find_element_by_id ( " Query " )
 # send Webdriver example by a key F12 
query.send_keys (Keys.F12)

2: a key operation by a combination of analog WebDriver

from the Selenium Import webdriver
 from selenium.webdriver.common.keys Import   Keys
 from selenium.webdriver.common.action_chains Import ActionChains
 Import Time 
Driver = webdriver.Chrome ()
 # access Baidu website 
driver.get ( " http://www.baidu. COM " )
 # by targeting the search input box id page elements 
kw_ele = driver.find_element_by_id ( " kW " )
 # input Be_your_own_hero in Baidu input box 
kw_ele.send_keys ( " Be_your_own_hero " )
the time.sleep (2 )
 # Select content Baidu search box 
ActionChains (Driver) .key_down (Keys.CONTROL) .send_keys ( " A " ) .key_up (Keys.CONTROL) .perform ()
 # shear Baidu search box of 
ActionChains (Driver) .key_down (Keys.CONTROL) .send_keys ( " the X- " ) .key_up (Keys.CONTROL) .perform () 
the time.sleep ( 2 )
 # access Baidu Home 
driver.get ( " HTTP: //www.sogou .com " )
 # get Sogou input box element object 
query_ele = driver.find_element_by_id ( " Query " ) 
ActionChains (Driver) .key_down (Keys.CONTROL) .send_keys ( " v").key_up(Keys.CONTROL).perform()

3: a combination of simulation cases by a third-party module

from the Selenium Import webdriver
 from selenium.webdriver.common.keys Import   Keys
 from selenium.webdriver.common.action_chains Import ActionChains
 Import win32con
 Import Win32API
 Import Time 
Driver = webdriver.Chrome ()
 # access Baidu website 
driver.get ( " HTTP: // www.baidu.com " )
 # mapped keyboard key corresponding to the key 
VK_CODE = {
     " Enter " : 0x0D ,
     " Ctrl " : 0x11,
     " A " : 0x41 ,
     " X " : 0x58 ,
     " V " : 0x56 
} 
# keyboard key is pressed 
def keyDown (KeyName): 
    win32api.keybd_event (VK_CODE [KeyName], 0,0,0) 
# keyboard release 
def a keyUp (KeyName): 
    win32api.keybd_event (VK_CODE [KeyName], 0, win32con.KEYEVENTF_KEYUP, 0) 
# by targeting the search input box id page elements 
kw_ele = driver.find_element_by_id ( " kW " )
 # input Be_your_own_hero input box in Baidu 
kw_ele.send_keys ( " Be_your_own_hero " )
the time.sleep ( 2 )
 # Select content Baidu search box 
keyDown ( " Ctrl " ) 
keyDown ( " A " ) 
keyUp ( " Ctrl " ) 
keyUp ( " A " ) 
keyDown ( " Ctrl " ) 
keyDown ( " the X- " ) 
keyUp ( " Ctrl " ) 
keyUp ( " the X- " ) 
the time.sleep ( 2 )
 # access Baidu Home 
driver.get("http://www.sogou.com " )
 # get Sogou input box element object 
query_ele = driver.find_element_by_id ( " Query " ) 
keyDown ( " Ctrl " ) 
keyDown ( " v " ) 
keyUp ( " Ctrl " ) 
keyUp ( " v " )

 

Guess you like

Origin www.cnblogs.com/Be-your-own-hero/p/11260780.html