This essay is a loose-set. No more forcing you to do it directly.
Step 1: use Webdriver to invoke the browser, Webdriver supported browsers for Firefox (), IE (), Chrome (), different browsers need to install different drivers.
This article uses the Firefox browser: Webdriver. Firefox (), open a URL by using the Get () function.
from Import Webdriver Import = webdriver. Firefox () driver.get ("http://172.18.xxxxxxxxxxxxx.jsp"# Maximize Browser display browser.set_window_size (800,600)# set browser display size browser.back ()# current browser back Browser.forward ()# current browser forward
Step2: Locating an Element object, you need to get a set of elements on the page. Method: Find_elements/find_element, unfamiliar classmates suggest using FIREBUG,F12 open
Web page, use the mouse to get the element object that needs to be positioned. such as Baidu's input box and Baidu positioning, can be the same ID to locate
#find_elements used to get a set of elements. find_elements_by_id (' xx ')#find the ID of an elementFind_elements_by_name (' xx ')#find the name of an elementFind_elements_by_class_name (' xx ')#find the class name of an elementFind_elements_by_tag_name (' xx ')#find the label name of an elementFind_elements_by_link_text (' xx ')#Find link text for an elementFind_elements_by_partial_link_text (' xx ')#find part of a link to an element of textFind_elements_by_xpath (' xx ')#To find the XPath of a child element within an elementFind_elements_by_css_selector (' xx ')#finds and returns a list of CSS selectors for multiple elements
Typically use find_element_by to locate a single element
You can use the for ... in ... Iterate over this set of elements
For checkbox in Checkboxes:checkbox.click ()
After we get to a set of elements, we can also use the Pop () method to get the first of the set of elements, and then manipulate the element:
find_elements_by_id (' xx '). Pop (). Click ()
- Pop () or pop (-1) Gets the last of a set of elements by default
- Pop (0) gets the first element of a set of elements by default
- Pop (1) Gets the second element of a set of elements by default
- ...... And so on
step3: Manipulating elements
Action object:
- · Click on Object
- · Send_keys analog key input on the object
- · Clear clears the contents of the object, if possible
- . Submit Submission Form
Actionchains class
- Context_click () Right-click
- Double_click () Double-click
- Drag_and_drop () drag
Webelement Other common methods:
- · Text gets the literal of the element
- · Submit Form
- · Get_attribute Getting property values
Example: Login Baidu
fromSeleniumImportWebdriverImportTimebrowser=Webdriver. Firefox () Browser.get ("http://ww.baidu.com")Print(Browser.title) browser.find_element_by_id ('kw'). Send_keys ("Selenium")#Baidu homepage Positioning input Box input SeleniumTime.sleep (5) browser.find_element_by_id ("su"). Click ()#Baidu homepage Positioning "Baidu a bit", clickTime.sleep (5) Browser.quit ()#Exit
Can join when positioning input
Time.sleep ()
Run too fast to join the sleep function
the real purpose of the function is not to show us the running process of the script, sometimes the network reason, or the page load is slow. Suppose the search box input box entered the selenium , the search button has not been loaded, then the script error. Adding time.sleep () to the appropriate location helps reduce the failure of script execution due to network causes;
Others
In Web applications, frame/iframe forms are often embedded in the application, Webdriver can only be on a single page element identification positioning, for the Frame/iframe form embedded page elements can not be directly located. This is required to switch the currently positioned body into an inline page of the Frame/iframe form through the Switch_to.frame () method.
Multi-layered frame or window positioning:
- Switch_to_frame ()
- Switch_to_window ()
Current_window_handle: Gets the current window handle
Window_handles: Returns the handle of all windows to the current dialog
Switch_to.window (Window Handle): Switches to the corresponding window.
As new topics are opened, a new window opens, so point to the new window, the topic window
handle = Driver.current_window_handledriver.switch_to_frame ("topframe") driver.find_element_by_id ("defaultframe"). Click () Driver.switch_to_window ( HANDLE)
Smart wait:
for a modern web application, there is often a frame (frame) or window ( window ) of the application, This also poses a challenge to our positioning.
sometimes we position an element, the locator is not a problem, but it has not been able to locate, it is necessary to check whether this element in a frame in which Seelnium Webdriver provides a The Switch_to_frame method can be very easy to solve this problem.
driver.webdriver.remote.webdriver.implicitly_wait (time_to_wait)implicitly waits for a vegan to be found or a command to complete; This method only needs to be called once per session
Driver.switch_to_alert ()
.Accept () # is equivalent to clicking OK, or using
driver.execute ("Acceptalert")
Driver.switch_to_alert ()
.dismiss () # is equivalent to clicking Cancel, or Using
driver.execute ("Dismissalert")
Driver.switch_to_alert (). Text #获取弹出框里的文字 or use
driver.execute ("Getalerttext") ["value"]
# Drag the page scroll bar to the bottom js="var q=document.documentelement.scrolltop=10000"Driver.execute_ Script (JS) time.sleep (3)
# move the scroll bar to the top of the page js="var q=document.documentelement.scrolltop=0"driver.execute_script (JS )
To invoke the keyboard key operation requires the introductionKeysPackage:From Selenium.webdriver.common.keys import keys
through Send_keys () Invoke key:
Send_keys (keys.tab) # TAB
Send_keys (keys.enter) # Enter
keyboard combination Key usage:
driver.find_element_by_id ("kw"). Send_keys (Keys.control,'a' ) Time.sleep (3) driver.find_element_by_id ("kw"). Send_keys ( Keys.control,'v')
Select all, paste
Cookies
- driver.get_cookies () obtained Cookies Information
- Add_cookie (cookie_dict) to Cookies Add session Information
- Delete_cookie (name) deletes a specific ( partial ) of the Cookies
- delete_all_cookies () Delete all cookie
# Get cookie Information cookie= driver.get_cookies ()# adds session information to the name and value of the cookie. Driver.add_cookie ({'name':'key-aaaaaaa') 'value':'value-bbbb'})
About verification Codes
The processing of the verification code is as follows:
1, the test first remove the verification code
2. Using Verification Code identification technology
3, the use of cookies to record login user name password, the next automatic login to eliminate the verification code input link
4, call the System interface (used during testing) to obtain the verification code
5. Call Sleep (10) When logging in, manual input manually.
Salted fish
Selenium + Python automation test (i)