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‘.

Locating Web Elements by id

0 votes

The most common attribute key names are class and id, where id has to be unique over the entire HTML page (according to the HTML specification). This locator strategy allows Selenium WebDriver to find that unique element and perform some action against it.

Example HTML:

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

We can use one of the following locators:

#username

If we want to be more explicit we can write:

input#username

Using the latter we explicitly mention the type of element.

 

Examine your knowledge and play the ‘locator game‘.

Access in-browser Development Tools

0 votes

Most common browsers have some built-in development tools. These tools will help you to reveal HTML of the page and understand how the page is structured. This all sounds good, but they all have their own look and feel. We will see how we can launch the `Developer Tools`.

Internet Explorer
In Internet Explorer we can access `Developer Tools` by pressing `F12`.

Firefox
Firfox has some built-in developer tools, but Firebug seems to be more popular and powerful. We can download the Firebug add-on from http://getfirebug.com/ .

The shortcut to open `Firebug` is `F12`.

FireBug toolbar

Click on the `Inspect` button on Firebug’s toolbar. Move around the page and everything beneath your mouse will be instantly revealed within Firebug, showing you the HTML and CSS behind it.

Chrome
Chrome comes with `Developer Tools`. The shortcut to open `Developer Tools` is `CTRL` + `SHIFT` + `I` or `F12`.

Chrome toolbar

Click on the `Inspect` button on Chrome Developer Tools toolbar. Move around the page and everything beneath your mouse will be instantly revealed within Chrome Developer Tools, showing you the HTML and CSS behind it.

Opera
Opera comes with `Opera Dragonfly`. The shortcut to open `Opera Dragonfly` is `CTRL` + `SHIFT` + `I`.

Safari
Safari has a built-in `Developer Tools`. First, you need to enable that you want to see the Develop menu.

Click on `Preferences…` in the `settings` drop down menu. Go to the `Advanced` tab and check `Show Develop menu in menu bar`.

The shortcut to open `Developer Tools` is `CTRL` + `ALT` + `I`.