Analog browser action, selenium module

. 1  Import Time
 2  from Selenium Import the webdriver
 . 3  from selenium.webdriver Import ActionChains
 . 4  
. 5  # Create a browser object, support for each browser, browser driver parameters 
. 6 Web = webdriver.Chrome (executable_path = ' chromedriver ' )
 . 7  
. 8  # command to open a web browser 
. 9 web.get (URL = ' https://www.baidu.com ' )
 10  "" " 
. 11  Find series:
 12      . In addition the results are returned to rest plurality by_id
 13 is      find_element_by_id     
 14     find_element_by_name
 15      find_element_by_xpath
 16      find_element_by_link_text
 . 17      find_element_by_partial_link_text
 18 is      find_element_by_tag_name
 . 19      find_element_by_class_name
 20 is      find_element_by_css_selector
 21 is  "" " 
22 is  
23 is  # by methods find series, acquired specified label 
24 SEARCH_INPUT web.find_element_by_id = ( ' kW ' )
 25  
26 is  # add content tag (Value ) 
27 search_input.send_keys ( ' baby cute ' )
 28 the time.sleep (2)
 29  
30  # Gets the label 
31 is BTN = web.find_element_by_id ( ' SU ' )
 32  
33 is  # of clicks tab 
34 is  btn.click ()
 35 the time.sleep (2 )
 36  
37 [  # execute js code 
38 is web.execute_script ( ' the window.scrollTo (0, document.body.scrollHeight) ' )
 39 the time.sleep (2 )
 40  # close the browser object 
41 is  web.close ()
 42 is  web.quit ()
 43 is  
44 is  
45  # action strand: 
46  #ActionChains is a method for automatically performing low-level interaction, such as a mouse movement, mouse button operation keys and context menu interaction. 
47  # This is useful to perform more complex operations, such as drag and hover. 
48  
49  # Create a motion chain 
50 Action = ActionChains (Web)
 51 is Action. The __init__ (Web)   # Create a new ActionChains. 
52 action.click ()   # click the element. 
53 action.click_and_hold ()   # left mouse button on the hold down elements. 
54 action.context_click ()   # execution context-click (right-click) on an element. 
55 action.double_click ()   # Double-click elements. 
56 is  
57 is action.drag_and_drop ( ' Source ', ' Target ' )   # left mouse button while holding down the source element, and then moved to the target element and release the mouse button. 
58  # parameters: source: mouse click elements. target: the mouse to move the elements. 
59  
60 action.drag_and_drop_by_offset ( ' Source ' , ' xoffset ' , ' yoffset ' )   # holding down the left mouse button on the source element, and then moved to the target shift and release the mouse button. 
61  # Parameters: source: mouse down element. xoffset: X-offset to the mobile. yoffset: to move the Y offset. 
62 is  
63 is action.move_by_offset ( ' xoffset ' , ' yoffset ' )   #Move the mouse to the current mouse position offset. 
64  # Parameters: xoffset: to move to the shift amount X, as a positive or negative integer. yoffset: To move to the Y offset, as a positive or negative integer. 
65  
66 action.move_to_element ( ' to_element ' )   # move the mouse to the intermediate element. to_element: To move to the WebElement. 
67  
68 action.move_to_element_with_offset ( ' to_element ' , ' xoffset ' , ' yoffset ' )   # The offset mouse to move the specified element. Offset with respect to the upper left corner of the element. 
69  # parameters: to_element: To move to the WebElement. xoffset: X-offset to the mobile. yoffset: to move the Y offset. 
70  
71 is action.pause (. 1)   #Pause a specified duration within a few seconds all the input 
72 action.release ()   # Release the mouse button on the element. 
73 is action.perform ()   # perform all stored. 
74 action.reset_actions ()   # Clear stored in the operation of the local and remote ends. 
75  
76 action.key_down ( ' value ' )   # send key only, without releasing it. It can only be used with the modifier keys (Control, Alt and Shift). 
77  # parameters: value: modifier keys you want to send. Values are defined in the class Keys. element: Send key element. If None, the element of interest will be sent to the current key. 
78  
79 action.key_up ( ' value ' )   # release modifier keys. 
80  #Parameters: value: modifier keys you want to send. Values are defined in the class Keys. element: Send key element. If None, the element of interest will be sent to the current key. 
81  
82 action.send_keys ( ' * keys_to_send ' )   # sends the key to the current focused element. keys_to_send: Key to be sent. Modifier key constants can be found in the "Keys" category. 
83  
84 action.send_keys_to_element ( ' Element ' , ' * keys_to_send ' )   # sends to the key element. 
85  # Parameters: element: transmitting the key element. keys_to_send: Key to be sent. Modifier key constants can be found in the "Keys" category. 
86  
87  # of example hold the C + Ctrl: 
88  # action.key_down (Keys.CONTROL) .send_keys ( 'c') KEY_UP (Keys.CONTROL) .perform () # hold the ctrl, and then add c release.

Code is divided into two parts:

  The above simple demonstration of a little open Baidu and search content, scroll down the screen, close the browser.

  The following is to create a chain of action, and include a method of operation of the chain, and to explain the usefulness, parameters.

Guess you like

Origin www.cnblogs.com/NoteBook3013/p/11110464.html