Webdriver provides a rich API, a variety of positioning strategies: id,name,css selectors, XPath, etc., where CSS selector positioning element efficiency is higher than XPath, using the Id,name attribute to locate the element is the most reliable and most efficient way.
1, tool selection: In our development test script in the process of each browser to us also provides a convenient positioning elements of the tool, I prefer to use the Firefox Firebug tool, is also a lot of development testers more enthusiastic choice, The reason is that Firefox is the only browser that can integrate the Selenium IDE, and Firebug gives users a rich set of extensions, we can choose according to their own needs, in general, the use of firebug+firefinder is enough to use, Firefinder supports XPath and CSS selector positioning elements, which is handy for helping us debug test scripts
2, Element positioning method: Findelement () and findelements ()
Findelement () This method returns the Webelement object based on the specified query condition, or throws an exception Eg:driver.findElement (By.id ("UserID") that does not match the condition;
Findelements () This method returns a collection of Webelement objects that specify the query criteria, or returns null
3. Various positioning element strategies provided by Webelement objects
ID:driver.findElement (By.id (<elementID>))
Name:driver.findElement (By.name (<elementName>))
ClassName:driver.findElement (By.classname (<elementClassName>))
TagName:driver.findElement (By.tagname (
LinkText:driver.findElement (By.linktext (<linkText>))
PartialLinkText:driver.findElement (By.partiallinktext (<partialLinkText>))
Css:driver.findElement (By.cssselector (<cssSelector>))
Xpath:driver.findElement (By.xpath (<xpathQuery>))
4. The Webelement class provides many ways to choose the most reliable and efficient method in our development scripts, and using Id,name is preferred because they are unique in HTML tags, so they are the most reliable
ID Locator: Driver.findelement (by.id ("username"))
Name Locator: Driver.findelement (By.name ("username"))
Class positioning: Driver.findelement (By.classname ("username"))
learn one more trick: The Webelement class supports querying sub-class elements, if there are duplicate elements in the page, but in different div, we can first navigate to its parent element, and then locate its child elements, as follows:
webelement Hello = driver.findelement (by.id ("Div1")). Findelement (By.lindtext ("Hello"));
5, using webelements to locate a number of similar elements, such as the existence of five radio buttons on the page, they have the same class attribute, the value is: MyRadio, we want to the five button loop operation, we can take them all out to put into the collection, and then do the loop operation, as follows:
List<webelement> radios = driver.findelements (By.classname ("MyRadio"));
for (int i = 0;i<radios.size (); i++) {
Radios.get (i). Click ();
}
Other positioning method and operation Id,name similar, here no longer repeat, next I focus on CSS selector and XPath under the description
The Webdriver by class provides the Cssselector () method, which is used in the following ways:
1. Locating elements using relative paths
For example, we would like to make the INPUT element in the DOM, we can do this, regardless of its position in the DOM, but there are some drawbacks, when there are multiple input elements in the DOM, the method always returns the first element in the DOM, which is not what we expect
Eg:webelement username = driver.findelement (by.cssselector ("input"));
In addition, in order to use this method to locate the element more accurately, we can combine the other properties of the element to achieve the purpose of precise positioning.
A, the combination of ID to locate, driver.findelement (By.cssselector ("Input#username")); Use the # connection between the tag and ID, if you know a friend of the CSS to see why this is written, do not understand it is okay, just remember this writing is OK
In addition, the method can be shortened to Driver.findelement (By.cssselector ("#username")); Sort of like the ID selector
b, use any attribute of the element to locate the element
Driver.findelement (By.cssselector ("tag name [property name = ' attribute value ']");
C, match partial attribute value
^= driver.findelement (by.cssselector ("tag name [property name ^= ' xxx ']"); Elements that match the value of the attribute to start with XXX
$= driver.findelement (by.cssselector ("tag name [property name $= ' xxx ']"); element that matches the value of the attribute with the end of XXX
*= driver.findelement (by.cssselector ("tag name [property name ^= ' xxx ']"); Match attribute value contains elements of XXX
2, using the relative + absolute path method, here is my own definition of the method, convenient memory, it is also true to achieve
Driver.findelement (By.cssselector ("Div#login>input")) in this method, "Div#login>input" is first positioned by a relative path to the DIV element with ID login. Then find its child element input (absolute path)
Second, the use of XPath positioning elements, compared to Cssselector,xpath is one of my more commonly used to locate elements, because it is very convenient, the disadvantage is that the consumption of system performance
1. Locating elements using absolute paths
Driver.findelement (By.xpath ("/html/body/div/form/input"))
2. Locating elements using relative paths
Driver.findelement (By.xpath ("//input")) returns the first found element that matches a condition
3, using the index positioning elements, the initial value of the index is 1, attention and array and so on separate
Driver.findelement (By.xpath ("//input[2)") returns the second found element that matches a condition
4. Combine attribute values to locate elements
Driver.findelement (By.xpath ("//input[@id = ' username ')");
Driver.findelement (By.xpath ("//img[@alt = ' flowr ')");
5. Use logical operators, combine attribute values to locate elements, and and OR
Driver.findelement (By.xpath ("//input[@id = ' username ' and @name = ' UserID ']");
6. Use attribute names to locate elements
Driver.findelement (By.xpath ("//input[@button]"))
7. Similar to Cssslector, use partial attribute values to match elements
Starts-with () driver.findelement (By.xpath ("//input[stars-with (@id, ' user ')])
Ends-with driver.findelement (By.xpath ("//input[ends-with (@id, ' name ')])
Contains () Driver.findelement (By.xpath ("//input[contains (@id," Ernam ")])
8. Match elements with any attribute value
Driver.findelement (By.xpath ("//input[@*= ' username ')")
9. Use the XPath axis to locate the element
Here is a little, see w3school.com
Third, using innertext positioning elements
1. Use Cssselector to find innertext positioning elements
Driver.findelement (By.cssselector ("span[textcontent= ' news]"));
2. Using the XPath text function
Driver.findelement (By.xpath ("//span[contains () (" Text (), ' Hello ')]) contains a matching
Driver.findelement (By.xpath ("//span[text () = ' News ']") absolute match
Selenium Webdriver Learning Summary-element positioning