Locating page elements
For many selenium commands, the target domain is required. Target recognizes the UI element within the Web page scope, and it uses the locatortype=location format. In many cases, locatortype can be omitted, and the following examples are used to describe various types of locatortype.
If so, there is an HTML code that reads:
html>
<body>
<form id= "LoginForm" >
<input name= "username" type= "text" /> <input name= "password" type= "password"/> <input name= "Continue" type= "
Submit" value= "Login" c8/>/>
<input name= "Continue" type= "button" value= "clear"/> <a "href="
> continue</a>
</form>
</body>
Let's take a look at how selenium offers those positioning methods:
1. Identifier positioning
This is the most common form of positioning, when it is not recognized as a different positioning method, the default is Dientifier positioning, under which the first page element that uses the ID will be recognized, and if no element with the specified ID is used, the element with the first name that matches the specified condition will be recognized.
The positioning strategy for identifier to identify HTML elements is as follows:
Identifier=loginform//Positioning page element from
Identifier=username//Position page element is username
Identifier=continue//Position page element is Continue
Because identifier positioning is the default, "identifier=" can not be written.
Continue//also means to locate page elements as Continue
2. ID Location
This positioning is narrower than the identifier location, and of course more specific, if you know the element ID feature, use this method:
Id=loginfrom//Positioning page elements from
3. Name positioning
The name positioning method will recognize the first UI element that matches the name attribute. If multiple elements have the same name attribute, you can use a filter to further refine your positioning 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//Positioning page element is Continue, type button
Tips:
The three types of locators allow selenium to be tested without relying on the position of the UI element on the page. So, when the page structure changes, the test can still pass. Sometimes, when designers change pages frequently, it is important to locate elements through 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 positioning methods, offering a number of possibilities, such as locating the third multiple-marquee on the page.
XPATH=/HTML/BODY/FORM[1]//absolute path (any minor changes in HTML will result in failure)
Form[1] The third form element in//html
xpath=//form[@id = ' loginform ']//id as loginfrom element
input[@name = ' username ']//input element and its name is ' username '
form[@id = ' loginform ']/input[1]//For form with ID ' loginform ', locate its first INPUT element
input[@name = ' Continue '] [@type = ' button ']//name as ' Continue ' and type ' button ' input
form[@id = ' loginform ']/input[4]//id the form of ' loingform ' to locate its fourth INPUT element.
Extended reading:
The Consortium XPath recommendation:http://www.w3.org/tr/xpath/
XPath tutorial:http://www.zvon.org/xxl/xpathtutorial/general/examples.html
http://www.w3.org/TR/xpath/
Firefox plugin, which can help you get XPath for page elements:
XPath Checker Firebug
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/