January 15, 2011

Locating an Element

Locating Techniques
Useful XPATH patterns
Text
Starts with
Contians
Siblings

starts-with
Many sites use dynamic values for element’s id attributes, which can make them difficult to locate. One simple solution is to use XPath functions and base the location on what you do know about the element. For example, if your dynamic ids have the format  where 12345 is a dynamic number you could use the following XPath://input[starts-with(@id, 'text-')]
contains
If an element can be located by a value that could be surrounded by other text, the contains function can be used. To demonstrate, the element  can be located based on the ‘heading’ class without having to couple it with the ‘top’ and ‘bold’ classes using the following XPath: //span[contains(@class, 'heading')]. Incidentally, this would be much neater (and probably faster) using the CSS locator strategy css=span.heading

Starting to use CSS instead of XPATH
Locating elements based on class
In order to locate an element based on associated class in XPath you must consider that the element could have multiple classes and defined in any order, however with CSS locators this is much simpler (and faster).
  • XPath: //div[contains(@class, 'article-heading')]
CSS: css=div.article-heading



No comments:

Post a Comment