Positioning page Elements
For many selenium commands, the target domain is required. Target identifies UI elements within the scope of the Web page, which uses the locatortype=location format. In many cases, locatortype can be omitted, and the following examples describe various types of locatortype.
If you have the following HTML code:
Html>
<Body>
<FormId= "LoginForm">
<InputName= "username"Type= "Text"/>
<InputName= "Password"Type= "Password"/>
<InputName= "Continue"Type= "Submit"Value= "Login"/>
<InputName= "continue" Type= "button " Value=" Clear "/>
<a href= "continue.html" >continue</< Span style= "COLOR: #800000" >a>
</form>
</body>
</html>
Let's see how selenium provides those targeting methods:
1. Identifier positioning
This is the most common way of positioning, when it is not recognized as the other positioning method, the default is Dientifier positioning, in this strategy, the first ID of the page element will be recognized, if the element with the specified ID is not used, then the first name will be identified with the specified criteria to match the element.
Identifier The positioning strategy for identifying HTML elements is as follows:
Identifier=loginform//Position page element as from
Identifier=username//Position page element is username
Identifier=continue//Position page element is Continue
Because identifier positioning is the default, "Identifier=" can be written out.
Continue//also means positioning the page element as Continue
2. ID Positioning
This positioning method is narrower than the identifier positioning range and, of course, more specific, if you know the element ID feature, use this method:
Id=loginfrom//Position page element from
3. Name Locator
The name positioning method will recognize the first UI element that matches the name property. If multiple elements have the same name attribute, you can use filters to further refine your targeting strategy. The default filter is value (matches the value feature):
Name=username//Position page element is username
Name=continue value=clear//Position page element is Continue, value is Clear
Name=continue Type=button//Position page element is Continue, type is button
Tips:
The three locators above allow selenium to test without relying on the position of the UI element on the page. So, when the page structure changes, the test can still pass. Sometimes it is important for designers to change pages frequently, by locating elements through the ID and name features.
4. XPath positioning
XPath is a language that locates elements in an XML document. Because HTML can be seen as an implementation of XML, selenium users use this powerful language to locate elements in a Web application.
XPath extends the above ID and name targeting methods, providing a number of possibilities, such as locating the third multi-box on a page.
XPATH=/HTML/BODY/FORM[1]//absolute path (any slight change in HTML will cause a failure)
Form[1] The third form element in//html
xpath=//form[@id = ' loginform ')//id element for Loginfrom
input[@name = ' username ']//input element and its name is ' username '
form[@id = ' loginform ']/input[1]//For a form with id ' loginform ', locate its first INPUT element
input[@name = ' Continue ' [@type = ' button ']//name is ' continue ' and type is ' button ' input
form[@id = ' loginform ']/input[4]//id is the ' loingform ' form and locates its fourth INPUT element.
Extended reading:
The recommendation:http://www.w3.org/tr/xpath/XPath
XPath tutorial:http://www.zvon.org/xxl/xpathtutorial/general/examples.html
http://www.w3.org/TR/xpath/
The Firefox plugin can help you get the XPath for the page element:
XPath Checker Firebug
5. Positioning via hyperlinks
You can locate the hyperlink by connecting the text, and if the two linked text is the same, the first match will be recognized.
Link=continue//Position page element Connection text is continue
6. Dom Positioning
The document Object Model is used to describe HTML documents that can be accessed using JavaScript.
This positioning strategy uses JavaScript to evaluate elements on the page, and you can use graduated symbols to simplify element positioning.
Because DOM positioning starts with "document", the "dom=" label is not required.
Dom=document.getelementbyid (' loginform ')//position page element form
dom=document.forms[' loginform '//position page element form
Dom=document.forms[0]//Position page element form
Document.forms[0]. Usernam//Positioning the page element username
Document.forms[0]. elements[3]//Position the page element continue, which is the fourth element of the form
7. CSS Positioning
CSS (cascading Style Sheets) is a language that is used to describe the performance of HTML and XML documents. CSS uses selectors to bind properties to page elements. These selectors can be used by selenium as a different positioning strategy.
Css=form#loginform//Position page element form
Css=input[name= "username"]//positioning the page element username
Css=input.required[type= "text"]//Position page element with type text
Css=input.passfield//Position page element with type password
css= #loginForm input[type= "button"]//Position the page element, which is of type button
css= #loginForm Input:nth-child (2)//Position page element Passfield, and it is the second input child element from
Extended reading:
http://www.w3.org/TR/css3-selectors/
Tip: Many experienced selenium users recommend CSS targeting because it is faster than XPath. You can also find more complex objects in the HTML file.
------------------------------------------------------------------------------
Rookie Tip:
So many ways to locate the page, or do not understand what to use?
Automated testing, we use the tool to complete the manual operation, if we want to click a button, we recognize that is a button, how to let the automation tool also recognize that is a button? How do I make a tool recognize a "OK" button, not a "Cancel" button? Each button has a different property, perhaps with exactly the same properties but with a different location. We have to describe them through their characteristics, and then the automation tools can find them based on our description.
So, what does it look like to switch to our automated test code?
The following is the implementation of the Java code
Selenium.click (" description of the element attribute");
Selenium.click ("id=loginform");
Selemiun.click ("name=continue value=clear");
......
-----------------------turn
Selenium IDE (v) Positioning page elements of the Selenium command