Selenium Automation's Webdriver API
See the Worm Division of "Automated Testing Practice" a book, learned a lot, thank you very much. Take note of the study notes for later study and use.
I. Element positioning
1.FIND_ELEMENT_BY_ID (); ID Locator
2.find_element_by_name (); Name Locator
3.find_element_by_class_name (); Class attribute positioning
4.find_element_by_tag_name (); Tag Property Positioning
5.find_element_by_link_text (); Text information before the element tag to locate
6.find_element_by_partial_link_text (); Take part of the text link to locate
7.find_element_by_xpath ();
Where XPath has multiple targeting strategies:
① Absolute path: Find_element_by_xpath ("html/body/div[2]/div[2]/div[3]/div[2]/form/input[1]");
② element properties: Find_element_by_xpath ("//input[@id = ' qwe ']");
Find_element_by_xpath ("//input[@name = ' qwe ']");
Find_element_by_xpath ("//input[@class = ' qwe ']");
Find_element_by_xpath ("//*[@id = ' qwe ']");
③ level attribute: Find_element_by_xpath ("//span[@class = ' qwe ']/input");
Find_element_by_xpath ("//form[@id = ' qwe ']/span[2]/input");
④ arithmetic logic: Find_element_by_xpath ("//input[@id = ' qwe ' and @class = ' qwer ']/span/input");
8.find_element_by_css_selector (); CSS selector positioning
CSS also has a variety of strategies:
①class Properties: Find_element_by_css_selector (". Qwe");
②id Properties: Find_element_by_css_selector ("#qwe");
③ tag Name: Find_element_by_css_selector ("input");
A. Parent-Child Relationship: Find_element_by_css_selector ("Span>input");
B. Attribute positioning: Find_element_by_css_selector (' [type= ' submit '] ');
C. Combination positioning: Find_element_by_css_selector ("form.fm>span>input>input.qwe");
9.BY element Positioning
The above mentioned 8 positioning methods, Webdriver also provides another set of wording, that is, the unified call Find_element () method, through by to declare the method of positioning, and the corresponding positioning method of the positioning parameters.
You need to insert the by class before using by:
From selenium.webdriver.common.by Import by
Find_element (by.id, "qwe");
Find_element (By.name, "qwe");
Find_element (by. Class_name, "qwe");
Find_element (by. Tag_name, "qwe");
Find_element (by. Link_text, "XXX Test Department");
Find_element (by. Partial_link_text, "Test Department");
Find_element (by. XPATH, "//* [@id = ' qwe ']");
Find_element (by. Css_celector, "Span>input");
Two. Controlling the browser
1. Control browser window Size
①set_window_size ();
such as Set_window_size (640,480); Width 640, Height 480
②maximize_window (); Window maximization, no parameters required, direct use
2. Control browser Advance and retreat
Back (); Return
Forwar (); Forward
such as Driver.back (); Which Driver=webdriver. Chrome ()
3. Simulate browser refresh
Refresh (); such as Driver.refresh () similar to F5
4. Simple element operation
①clear (); Clear text
②send_keys (*value); Analog Key input
③click (); Click Element
Example:
driver.find_element_by_id ("Qwe"). Clear ()
driver.find_element_by_id ("Qwe"). Send_keys ("Zhang")
DRIVER.FIND_ELEMENT_BY_ID ("Login"). Click ()
5.webelement Interface Common Methods
Size: Returns the dimension of the element
Text: Gets the literal of the element
Get_attributte (name): Get property
Is_displayed (): Sets whether the element is visible to the user
Example:
size=driver.find_element_by_id ("Qwe"). Size gets the dimensions of the input box
text=driver.find_element_by_id ("Qwe"). Text gets textual information
Attributte = driver.find_element_by_id ("Qwe"). get_ attributte (' type ') can be an ID, name, type, or any other property
result= driver.find_element_by_id ("Qwe"). Is_displayed () returns whether the result of the element is visible, returns a result of ture, or false
5. Mouse events
Perform (): Performs the behavior stored in all Actionchains
Context_click (): Right-click
Double_click (): Double-click
Drag_and_drop (Element,target
Move_to_element (): Mouse hover
Example:
①romselenium.webdriver.common.action_chains import Actionchains first introduced Actionchains class
Driver =webdriver. Chrome ()
Driver.get ("http://www.baidu.com")
#...
#定位到要右击的元素
right_click= driver.find_element_by_id ("Qwe")
#对定位到的元素执行鼠标右击操作
Actionchains (Driver). Context_click (Right_click). Perform ()
Description
Actionchains (Driver): Call this class to pass the browser driver driver as a parameter
Context_click (Right_click): Simulates a mail operation that needs to specify element positioning when used
PERFOM (): Specifies the behavior that is stored in all actionchains.
②above=driver.find_element_by_id ("Qwer")
Actionchains (Driver). Move_to_element (above). PERFOM ()
Other methods are similar
6. Keyboard events
The Keys class provides all the key methods on the keyboard
Fromselenium.webdriver.common.keys Import Keys
Send_keys (keys.back_space) Delete key
Send_keys (keys.space) space key
Send_keys (keys.tab) Tab key
Send_keys (keys.escape) fallback key
Send_keys (keys.enter) Enter
Send_keys (Keys.control, ' a ') Select all
Send_keys (Keys.control, ' C ') copy
Send_keys (Keys.control, ' x ') clip
Send_keys (Keys.control, ' V ') paste
Send_keys (KEYS.F1)
...
Send_keys (KEYS.F12) f1-f12
7. Get Verification Information
Title,url, text
Example:
Now_url=driver.current_url get the current page URL
Title=driver.title Gets the current page title
Text is already mentioned in the front, do not repeat
8. Set element wait
① Explicit wait: webdriverwait (Driver,poll_fequency=0.5,ignored_exceptions=none)
② implicit wait: driver.implicitly_wait (time) time can be customized
③sleep hibernation: Sleep time time can be customized
9. Positioning a group of elements
After locating elements of a single element, add s
FIND_ELEMENTS_BY_ID ();
Find_elements_by_name ();
Find_elements_by_class_name ();
Find_elements_by_tag_name ();
Find_elements_by_link_text ();
Find_elements_by_partial_link_text ();
Find_elements_by_xpath ();
Find_elements_by_css_selector ();
10. Multi-form switching
Driver.swtich_to.frame ()
11. Multi-Window switching
Driver.switch_to.widow () to switch to the appropriate window
Current_window_handle gets the current window handle
Window_handles returns the handle of all windows to the current session
12. Warning Box handling
Text: Returns the text message in the Alert/confirm/prompt
Accept (): Accepts an existing warning box
Dismiss (): Dissolve Existing warning box
Send_keys (keystosend): Send Text to Warning box
13. Uploading Files
Normal upload: Place the path of the local file as a value in the Input tab and submit this value to the server through the form form
Plugin upload: The upload function is implemented based on Flash,javascript or Ajax technology.
①send_keys ()
Such as:
From selenium import Webdriver
Import OS
Driver = Webdriver. Chrome ()
File_path= ' file:///' + os.path.abspath (' upfile.html ')
Driver.get (File_path)
#定位上传按钮, add a local file
Driver.find_element_by_name ("file"). Send_keys (' D:\\upload_file.txt ')
Driver.quit ()
②autoit implement upload and download using http://www.autoitscript.com/site/
14. Download the file
From selenium import Webdriver
Import OS
Fp=webdriver. Firefoxprofile ()
Fp.set_preference ("Browser.download.folderList", 2) #0是默认路径, 2 is the specified path
Fp.set_preference ("browser.download.manager.showWhenStarting", False) #是否显示开始
Fp.set_preference ("Browser.download.dir", OS.GETCWD ()) #用于指定所下载的文件的目录
Fp.set_preference ("Browser.helperApps.neverAsk.saveToDisk", "Application/octet-stream") #下载文件的类型
Driver=webdriver. Firefox (FIREFOX_PROFILE=FP)
Driver.get ("Http://pypi.Python.org/pypi/selenium")
Driver.find_element_by_partial_link_text ("Selenium-2"). Click ()
15. Manipulating Cookies
Webdriver How to manipulate cookies:
Get_cookies (): Get all cookie information
Get_cookie (name): Returns the cookie information for the dictionary key "name"
Add_cookie (cookie_dict): Add a cookie. Cookie_dict is a Dictionary object and must have a Name,value value
Delete_cookie (name,optionsstring): Delete Cookie Information
Delete_all_cookies (): Delete all cookie information
16. Call Javascipt
Adjust browser scroll bar position
Window.scrollto (left margin, top margin)
17. Handling HTML5 Video Playback
Load (), play (), pause () load, play, pause
18. Windows
Driver.get_screenshot_as_file ("D:\\xxxxx") #截取当前窗口 and specify where to save the picture
19. Close the window
Quit (): Exits the relevant program and closes all windows;
Close (): Close the current window
20. Processing of verification codes
① Remove the verification code
② Set Universal Verification code
③ Verification Code Identification technology
④ Record Cookies
20170627Selenium Automation---Webdriverapi