In the UI level of automated test development, the positioning and operation of elements is the basis, but also often encountered difficulties. Webdriver offers 8 types of positioning:
1. ID Locator: FIND_ELEMENT_BY_ID ("id value"); id attribute is unique
" loginname " 2 driver.find_element_by_id ( " loginname " ). Send_keys ("admin") 3 driver.find_element_by_id ( " pwdtip " " pwdtip " ). Send_keys ("111111" )
2. Name Locator: The name of the element, Find_element_by_name ("Name value"); The Name property value may not be unique on the current page
1 driver.find_elements_by_name ("periodname") [1].click ()# Select section: Junior High School 2 driver.find_elements_by_name ("subjectname") [0].click () # Selected disciplines: Chinese
Find_elements_by_name ("Periodname") is because the current page has a set of RadioButton name value is Periodname, so you can locate a set of elements of the method findelements, It's a list of results.
3. Class Positioning: The class name of the element, Find_element_by_class_name ("Class value")
Driver.find_elements_by_class_name ("u-btn-levred") [0].click ()# Select grade: Seven Grade
4. Tag positioning: The various tags under the HTML document of the page, Find_element_by_tag_name ("input");
Tag is often used to define a class of functions, so the probability of identifying an element through tag is very low. Open a page, you will find a large number of <div>, <input>, <a> and other tags, so tag name positioning is rarely used
5. Link positioning: Specifically used to locate text links, find_element_by_link_name ("text");
Driver.find_element_by_link_text (U" exit "). Click () #页面右上方的一些个人操作, such as exit, personal center, message notification, etc.
6. Partial link positioning: is a supplement to link positioning, when the text content on the link is longer, you can take part of the text to locate, of course, this part can uniquely identify the link
※ Note : The above way is slightly limited, and often the page does not id,name these attribute values, class name is high repeatability, link positioning is targeted, so XPath and CSS positioning more flexible.
7. XPath positioning: Find_element_by_xpath (""), there are a variety of positioning strategies, with the Firepath plug-in automatically generated by the following several ways
1) Absolute path positioning: For no id,name, classname bad positioning, which is also my most commonly used, because you can use the Firefox Firepath plugin can easily get to the XPath value
2) Use element attribute to locate:
Find_element_by_xpath (".//*[@id = ' Title ']"), here is the ID, or the element can be used to uniquely identify the attributes, not limited to the ID, name, class these; * represents the tag name, you can use * instead of when not specified
3) combination of levels and attributes: This is the
4) Using logical operators
1 driver.find_element_by_xpath (".//*[@id = ' Divword ']/input[7]"). Click ()# Login 2 driver.find_element_by_xpath ("html/body/div[4]/div/div[2]/div/div[3]/a[ 1]. Click ()# Personal page Release course action
8. CSS positioning (weak, very little, but very powerful, simple and flexible than XPath): Using selectors to bind attributes to page elements gives you the flexibility to select any property of the control; Find_element_by_css_selector ("") You can also use Firepath to generate CSS!
1) Navigate by class attribute: Dot (".") ) indicates that it is positioned through the class attribute
1 class="u-btn mart5" type="submit" onclick= " return User.check ()" value=" login >2 Driver.find_element_by_css_selector (". U-btn.mart5"). Click ()
2) location by id attribute: ("#") means the element is positioned by ID
Driver.find_element_by_css_selector ("#loginName")
3) by other attributes: ("[]"), the attribute in brackets can uniquely identify this element, the value of the property can be quoted, or can not be added
1 class="u-btn mart5" type="submit" onclick= " return User.check ()" value=" login >2 Driver.find_element_by_css_selector ("[type=submit]"). Click ()
4) Combination Positioning
Usually use the generated xpath,id,name,classname these more, today according to the recent practice of this time, and reference to the book on the next, found that the original XPath and CSS there are so many ways, incidentally, some of the recent code test, some simple CSS positioning can be successful , some Firepath generated is not available, some combination positioning needs to be studied, is somewhat difficult. Finally record a positioning method, closer to the bottom of the implementation of the positioning, but the book said Webdriver more recommended in front of those writing, for Mao pinch?
9. Positioning elements with by
In addition to find_element_by_*** this way, there is another set of methods, that is, the unified call Find_element () method, two parameters, the first parameter is the type of positioning, by the by provided; the second parameter is the specific value of the anchor
fromSelenium.webdriver.common.byImportby #使用By这种定位前要将By类导入find_element (By.id,"LoginName") find_element (By.name,"Subjectname") find_element (By.class_name,"u-btn-levred") find_element (By.tag_name,"input") find_element (By.link_text,"Exit") find_element (By.partial_link_text,"back") find_element (By.xpath,".//*[@id = ' Title") find_element (By.css_selector,"[Type=submit]")
Webdriver api--8 ways to locate elements