Locating Web Elements by index matching

0 votes

Probably this is not the best way of locating elements, but sometimes it’s needed to use these kind of locator strategy. A reason might be the lack of element identifiers.

CSS XPath
Select second element E:nth-child(2) //E[2]
First <E> child E:first-of-type //E[1]
Last <E> child E:last-of-type //E[last()]

 

Examine your knowledge and play the ‘locator game‘.

Please follow and like us:
Follow by Email
Facebook
Google+
http://www.seleniuminaction.com/locating-web-elements-by-index-matching/
Twitter
LinkedIn

Locating Web Elements by label matching

0 votes

Not always you want to take a look at the underlying HTML code. Therefore it can be handy to define the text of the label in front of form fields. The following example defines the use of label matching.

Example HTML:

<label for="lastname">Last Name</label>
<input type="text" id="lastname" />

We can use:

`//label[contains(text(), 'Last Name')]/../input`

It can be a bit tricky as the label can be defined before, after or around the form fields.

 

Examine your knowledge and play the ‘locator game‘.

Please follow and like us:
Follow by Email
Facebook
Google+
http://www.seleniuminaction.com/locating-web-elements-by-label-matching/
Twitter
LinkedIn

Locating Web Elements by text matching

1 vote

Although it is not the best solution, sometimes we need to locate elements based on the text displayed. It’s not the best solution if you have to test a multilingual website, because the text is different in every language.

Using XPath we have the following options:

Text equals

//E[text() = 'Displayed text']

Text contains

//E[contains(text(), 'Displayed text')]

 

Examine your knowledge and play the ‘locator game‘.

Please follow and like us:
Follow by Email
Facebook
Google+
http://www.seleniuminaction.com/locating-web-elements-by-text-matching/
Twitter
LinkedIn

Locating Web Elements by attribute/wildcard matching

0 votes

Sometimes we don’t have full control over the key/value pairs used in the HTML. For example, if you have to test a COTS (Commercial Of The Shelf) application or if you are tight to framework specific things, such as JSF or ASP.NET. In this case ids, or parts, are generated and tend to be long and brittle as they might contain generated numbers. A few examples:

ASP.NET

<input id="ctl00_ContentPlaceHolder1_CustomUserControl1_Username" type="text" />

If you change the ClientIDMode property of the control to Static then you will see an id with the following value Username

JSP

<input id="j_id819:inputSequencenumber" type="text" />

In these circumstances you might want to use wildcards, so you will end up with less brittle locators.
Wildcards explained

CSS XPath
Equals E[a=v] //E[@a=v]
Contains E[a*=v] //E[contains(@a, ‘v’)]
Starts-with E[aˆ=v] //E[starts-with(@a, ‘v’)]
Ends-with E[a$=v] //E[ends-with(@a, ‘v’)]

You can define the following locators for the mentioned HTML elements:

input[id$='Username']

and

input[id$='Sequencenumber']

 

Examine your knowledge and play the ‘locator game‘.

Please follow and like us:
Follow by Email
Facebook
Google+
http://www.seleniuminaction.com/locating-web-elements-by-attribute-wildcard-matching/
Twitter
LinkedIn