Selenium Webdriver Learning Summary-element positioning

Source: Internet
Author: User
Tags tagname xpath

Selenium Webdriver Learning Summary-element positioning Webdriver provides a rich API with a variety of positioning strategies: Id,name,css selector, XPath, etc., where CSS selector positioning element efficiency is higher than XPath, using ID, The name attribute is the most reliable and efficient way to locate elements. 1. Tool Selection:In our development TestScript 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, but also many developers are more enthusiastic choice, because Firefox is the only one to integrate SeleniumIDE's browser, and Firebug to provide users with a wealth of extension components, 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 (< htmltagname>) 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 methods, how to choose the most reliable and most efficient method in our development script, using Id,name is the preferred, because they are unique in the HTML tag, so is the most reliable ID positioning: driver.findelement (by.id ("username"))  Name positioning: driver.findelement (By.name ("username")) class positioning: Driver.findelement (By.classname ("username")) Learn a trick: the Webelement class supports querying subclass 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,For example, there are five radio buttons on the page, they have the same class attribute, the value is: MyRadio, we want to loop over the five buttons, we can take them all out to put in 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 pathsFor 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 more accurate positioning elements, we can combine the other attributes of the element to achieve the purpose of precise positioning a, combined with the ID to locate, driver.findelement (By.cssselector ("Input#username")); Between the label and ID using the # connection, if the CSS to understand the friend a look will know why this is written, do not understand it is okay, just remember this writing is OK also this method can be abbreviated to Driver.findelement ("# By.cssselector Username ")); Sort of like the ID selector

B, use any attribute of the element to locate the elementDriver.findelement (By.cssselector ("tag name [property name = ' attribute value ']"); c, Match partial attribute value
^= driver.findelement (by.cssselector ("tag name [property name ^= ' xxx ']");  Match attribute values to the element starting with xxx $= driver.findelement (by.cssselector ("tag name [property name $= ' xxx ']);  The element that matches the attribute value ends with xxx *= driver.findelement (by.cssselector ("tag name [property name ^= ' xxx ']); Match attribute value contains elements of XXX
   2. Using relative + absolute path method, here is my own definition of the method, convenient memory, it is also true to achieve the Driver.findelement (By.cssselector ("Div#login>input") in this method "Div#login>input" First locate the DIV element with ID login by relative path, then find its child element input (absolute path) second, using XPath to locate elements, compared to Cssselector,xpath is a more common way of locating elements, because it is very convenient, the disadvantage is that the consumption of system performance 1. Locating elements using absolute pathsDriver.findelement (By.xpath ("/html/body/div/form/input")) 2. Locating elements using relative pathsDriver.findelement (By.xpath ("//input")) returns the first found element that matches a condition 3, using index positioning elements, the initial value of the index is 1, attention and array, etc.Driver.findelement (By.xpath ("//input[2)") returns the second found element that matches a condition 4. Combine attribute values to locate elementsDriver.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 orDriver.findelement (By.xpath ("//input[@id = ' username ' and @name = ' UserID ']"); 6. Use attribute names to locate elementsDriver.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.XPA Th ("//input[ends-with (@id, ' name ')]")) contains () Driver.findelement (By.xpath ("//input[contains (@id," Ernam ")])
   8. Match elements with any attribute valueDriver.findelement (By.xpath ("//input[@*= ' username ')") 9. Use the XPath axis to locate the elementHere is a little, see w3school.com third, using innertext positioning elements    1. Use Cssselector to find innertext positioning elementsDriver.findelement (By.cssselector ("span[textcontent= ' news]")); 2. Using the XPath text functionDriver.findelement (By.xpath ("//span[contains (Text (), ' Hello ')]) contains a matching driver.findelement (By.xpath ("//span[text () = ' news ')) absolute match transfer from: http://www.blogjava.net/qileilove/archive/2013/12/25/408002.html

Selenium Webdriver Learning Summary-element positioning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.