First, the mouse
Element object parameters are, in addition to the functions related to the coordinate;
Function name: ActionChains action chain
Common Mouse operation:
1 ) was suspended move_to_element 2 ) Click the Click . 3 ) Double-DOUBLE_CLICK . 4 ) right context_click . 5 ) drag drag_and_drop . 6 ) suspended PAUSE . 7) Input send_keys
Steps:
1) to be executed all mouse movements, first put a list of them.
2) perform (): mouse actions performed.
Example:
1 Example: 2 provided in the home Baidu found in Advanced Settings ((Advanced disposed after the mouse was suspended in the set)) . 3 . 4 Import Time . 5 . 6 from Selenium Import the webdriver . 7 from selenium.webdriver.common.action_chains Import ActionChains . 8 from selenium.webdriver.common.by Import By . 9 from selenium.webdriver.support.wait Import WebDriverWait 10 from selenium.webdriver.support Import expected_conditions AS EC . 11 12 is Driver =webdriver.Chrome () 13 is driver.maximize_window () 14 driver.get ( " https://www.baidu.com/ " ) 15 16 # . 1, find the elements (Baidu home setting) to operate the mouse . 17 LOC = ( By.XPATH, ' // div [@ ID = "U1"] // A [@ name = "tj_settingicon"] ' ) 18 is ELE = driver.find_element (* LOC) . 19 ele.click () 20 is 21 is # 2, examples of class Actonchains 22 is AC = ActionChains (Driver) 23 is 24 # . 3, call mouse behavior (suspension) 25 ac.move_to_element (ELE) 26 is 27 #4, calls the perform () performs mouse operation 28 ac.perform () 29 the time.sleep (2 ) 30 31 is # # Note: if the operation can be combined with 2,3,4 32 # ActionChains (Driver) .move_to_element (ELE ) .perform () 33 is 34 is # . 5, the drop-down list element is visible waiting 35 LOC2 = (By.XPATH, ' // A [text () = "advanced Search"] ' ) 36 WebDriverWait (Driver, 10 ) .until ( EC.visibility_of_element_located (loc2)) 37 38 # 6, select the element you want to operate 39 # trigger advanced search content that appears 40 driver.find_element (* loc2) .click ()
Second, the operation of the drop-down list (Select category)
Select operation using Class
1) Initialization, select objects pass;
2) select a value according to the index, value attribute, text content;
Example:
Operating on the basis of the above cases (for Baidu advanced settings of the file format drop-down box to select operation)
. 1 from selenium.webdriver.support.select Import the Select 2 . 3 # . 1, initialize, select objects pass . 4 LOC3 = (By.XPATH, ' // select [@ name = ". Ft"] ' ) . 5 WebDriverWait (Driver, 10 ) .until (EC.visibility_of_element_located (LOC3)) . 6 select_element = driver.find_element (* LOC3) . 7 . 8 S = the select (select_element) . 9 10 # 2, in accordance with the standard value attribute, text to select the value 11 # the subscript selected from the value 12 is s.select_by_index (. 6 ) 13 is the time.sleep (. 3 ) 14 # The value attribute value is selected from 15 s.select_by_value ( " DOC " ) 16 the time.sleep (. 3 ) . 17 # The value selected from the contents of the file 18 is s.select_by_visible_text ( ' the RTF file (.rtf) ' ) . 19 the time.sleep (. 3 ) 20 is 21 is # 3, closes the session 22 is the time.sleep (. 5 ) 23 is driver.quit ()
Third, the keyboard (Keys class)
Keys class processing special keys
Example:
After finding the search input box, type in the Baidu home page, click on Baidu, mouse operation using the enter key replacement
. 1 Import Time 2 from Selenium Import the webdriver . 3 from selenium.webdriver.common.keys Import Keys . 4 . 5 # . 1, open access home Baidu . 6 Driver = webdriver.Chrome () . 7 driver.maximize_window () . 8 driver.get ( " HTTP: //www.baidu.com " ) . 9 # 2, find the search input box selenium webdriver, press enter to search 10 Element driver.find_element_by_id = ( " kW " ) . 11 element.send_keys ( "selenium webdriver", Keys.ENTER) 12 element.send_keys(Keys.CONTROL,"c") 13 14 # 3、关闭会话 15 time.sleep(5) 16 driver.quit()