Browser actions
12345678 |
# Refresh driver. Refresh() # forward driver. Forward() # back driver. Back() |
Get tag Element
1234567891011121314151617181920212223 |
# Locate the target element by IDdriver. find_element_by_id(' I1 ') # Locating target elements via classnamedriver. Find_element_by_class_name(' C1 ') # Locating the target element with the Name propertydriver. Find_element_by_name(' N1 ') # Locating target elements with XPathdriver. Find_element_by_xpath('//*[@id = ' i1 ') # positioning target elements via CSS selectordriver. Find_element_by_css_selector(' #i1 ') # by Tag name positioning (Note: In a page, the label must be repeated, so do not use this to locate)driver. Find_element_by_tag_name(' input ') # Find elements through text in tagsdriver. Find_element_by_link_text(' login ') # Search by fuzzy matching of text in tagsdriver. Find_elements_by_partial_link_text(' record ') |
Get tag elements commonly used a total of 8 positioning methods, and selenium actually provides 18 positioning methods, and 8 is the above plural form, here does not introduce, the actual use is not commonly used, there are 2 is actually the above said 16 of the underlying package. A method of invocation that is parameterized.
Cookie manipulation
1234567891011121314 |
# based on Cookiekey, get cookie informationcookie = driver. Get_cookie(' Cookiekey ') # Get all cookie informationcookies = driver. Get_cookies() # Add a cookie, in strict accordance with the format, the cookie key is Name,value valuedriver. Add_cookie({' name ': 'tmp ',' value ':' 123123123 '}) # Delete all cookie informationdriver. Delete_all_cookies() # Delete the corresponding cookie according to Cookiekeydriver. Delete_cookie(' UICode ') |
Window operations
1234567891011121314151617181920 |
# Get the size of the current browserdriver. Get_window_size() # Set the browser size by pixeldriver. Set_window_size(' width ',' height ') # Gets the coordinates of the current window's location for Windows x, ydriver. Get_window_position() # Sets the location of the current window for Windows, x, ydriver. Set_window_position(a) # Maximize the current window without the need to pass the parameterdriver. Maximize_window() # Returns the browser handle of the current operationdriver. Current_window_handle # returns all browser handles for open serverdriver. Window_handles |
Intercept current Page
12345 |
# Get the binary picture data of the current page, need to write the file yourself driver. Get_screenshot_as_png() # As_png in the upper package, only need to pass in the picture name automatically written as a picture driver. Get_screenshot_as_file(' filename.png ') |
Executing javascript statements
1234567 |
# execute JavaScript statements driver. Execute_script(' JavaScript commond ') # example:# using JS to manipulate scroll bars # parameter 1:x parameter 2:y window. ScrollTo(+); |
Close and exit
12345 |
# When multiple is turned on, close the current page driver. Close() # Exits and closes all page-driven driver. Quit() |
Other
1234567891011 |
# back to page source driver. Page_source # Returns the tag header driver. Title # returns the current URLdriver. Current_url # Get browser name such as: Chromedriver. Name |
Elementapi interface
1234567891011121314151617181920212223242526272829303132333435363738 |
# Gets the property value based on the Tag property nameelement. Get_attribute(' style ') # Enter a string into the input box if the type of input is a file type, you can enter an absolute path to upload the fileelement. Send_keys() # Clear Text contentelement. Clear() # left mouse button click actionelement. Click() # Get Properties by property nameelement. Get_property(' id ') # Returns whether the element is visible True or Falseelement. Is_displayed() # Returns whether the element is selected True or Falseelement. is_selected() # Returns the name of the tag elementelement. Tag_name # Gets the width and height of the current labelelement. Size # Gets the text content of the elementelement. Text # Copy the return button to submit the dataelement. Submit() # Gets the coordinates of the current elementelement. Location# Capturing Pictureselement. Screenshot() |
Common exceptions
123456789101112131415 |
nosuchelementexception: no element found nosuchframeexception: No iframe found nosuchwindowexception: Window handle not found handle nosuchattributeexception: Property Error noalertpresentexception: No alert pop-up box found elmentnotvisibleexception: Element not visible elementnotselectableexception: Element not selected timeoutexception: Find element Timeout |
Web Automation Test-d3-Learning Note II (SELENIUM-WEBDRIVERAPI interface)