Selenium Automation Element Identification Options


Selenium Automation Element Identification Options

Locating by Identifier:  A generic catch-all approach for when no recognized locator type is used. With this strategy, the first element with the id attribute value matching the location will be used. If no element has a matching id attribute, then the first element with a name attribute matching the location will be used.

Locating by ID: This type of locator is more limited than the identifier locator type, but also more explicit. Use this when you know an element’s id attribute. Example: <form id="loginForm">

Locating by Name: The name locator type will locate the first element with a matching name attribute. If multiple elements have the same value for a name attribute, then you can use filters to further refine your location strategy. The default filter type is value (matching the value attribute). Example: <input name="username" type="text" />

Locating by Xpath: XPath is the language used for locating nodes in an XML document. As HTML can be an implementation of XML (XHTML), Selenium users can leverage this powerful language to target elements in their web applications. XPath extends beyond (as well as supporting) the simple methods of locating by id or name attributes, and opens up all sorts of new possibilities such as locating the third checkbox on the page. Example: //form[@id='loginForm']/input[4]

Locating Hyperlinks by Text: This is a simple method of locating a hyperlink in your web page by using the text of the link. If two links with the same text are present, then the first match will be used. Example: <a href="continue.html">Continue</a>

Locating by DOM: The Document Object Model represents an HTML document and can be accessed using JavaScript. This location strategy takes JavaScript that evaluates to an element on the page, which can be simply the element’s location using the hierarchical dotted notation. Example: dom=document.getElementById('loginForm')

Locating by CSS: CSS uses Selectors for binding style properties to elements in the document. These Selectors can be used by Selenium as another locating strategy. Example: css=#loginForm input:nth-child(2)

Locating by Label: Locate form elements in selenium tests by matching the text in their associated labels. Example: <label>Nested Input: <input id="nestedInput" /></label>


NOTE: Selenium has the ability to use JavaScript RegExp in most location strategies, so things like partial ID’s might work. Xpath RegExp is used in Xpath, not JavaScript RegExp.