top of page

HTML and Selenium. An introduction

HTML elements

An HTML element has the following syntax:

 <element  /> 

An HTML element in enclosed within a preceding '<' and a trailing '/>' characters. An HTML element can have none, one or many attributes, depending on its' type. The attribute has pairs of keys and values to define additional information about the element.

There are a few attributes that can be assigned to any HTML element. Two of them are described here:

  1. ID - to specify a unique identifier for the element.

  2. CLASS - an element can have one or more classes.

There are a great number of HTML elements, but for the purpose of Selenium testing, the most commonly used ones are presented below.

Common HTML elements



Element: a. Attributes:

  1. href: the URL that will open when the link will be clicked. The value of this attribute is a string. Example:

 <a href="" />  
  1. target: where the link will open. This attribute can only have a subset of predefined values: _blank (the link opens in a new window or tab), _self (default - opens in the same frame), _parent (opens the link in the parent frame), _top (opens in the same window).  A frame represents an area of the whole page, delimited by the <frame> element. Example:

 <a href="" target="_top" /> 


Element: select. Syntax:

<option value="value1">Value 1</option>
<option value="value2">Value 2</option>
<option value="valuen">Value N</option>

This will create a dropdown that contains the following values, in this order: Value 1, Value 2, Value N. Each element, as can be seen here, has a corresponding 'option' tag, to create an entry in the dropdown.



Element: img. Attributes:

  1. src: the source URL from where the image is to be loaded.

  2. height: specifies to what height should the image be resized. If not specified, the height will remain the default one (the height of the actual image, as it appears at the 'source' location).

  3. width: specifies to what width should the image be resized. If not specified, the width will remain the default one (the width of the actual image, as it appears at the 'source' location).


 <img src="theSourceUrl" height="500" widht="320" /> 


There are three different types of lists:

  1. numbered lists are made up of an opening <ol> and a closing </ol> tags, within which each list element is represented by a <li> and </li> pair of tags. Example:

  1. unordered lists are made up of <li> and </li> tag pairs, as many as the elements of the list. Example:

  1. bulleted lists are made up of an opening <ul> and a closing </ul> tag, within which each list element is represented by <li> and </li> pairs of tags. Example:


Grouping elements

To group elements in the HTML page for structuring purposes, <div>, <span> and <table> elements.



An input element can be of several types. It is included in <form> elements and is used for allowing users to input data, data which is then submitted and processed. An example of a form can be a page where a user is asked to provide his personal details in order to create an account on a site, to purchase something online, and so on. Some of the available inputs are described below. For each of them the syntax is the same (but some of them have different attributes that they can declare):

 <input type="theType"  /> 

Text area: By using the type="text" attribute, you will define an area in which you can type text.

Submit button: By using type="submit", you will declare a button that will submit the form, once you click it. Once submitted, the data the user has entered on the form is submitted to be processed.

Checkbox: By using type="checkbox" you will create a checkbox that is by default not selected. It can be selected/unselected by clicking on it.

Email field: By using type="email", you will create an input element into which you need to enter text, that is validated against a valid email format. If the text typed into this input does not match the expected email format, a gentle reminder is displayed next to the field.

File upload: By using type="file", you will define an element that has a 'Browse' button, allowing you to upload a file from your local computer.

Password: By using type="password", you will create an input field into which you can enter text. While you type, the letters will not actually be seen inside the field, but instead '*' characters will be displayed, to mask the characters that were typed into the field.

Radio button: By using type="radio", radio buttons can be created.

There are different attributes that can be used with an input, depending on which type of element you want to create, but there  are also some common ones. For example:

  1. checked="checked": if an element is selected (checked) by default (for radio buttons and checkboxes).

  2. disabled="disabled": if an element should be disabled and should not be interacted with. For example, in the case of a checkbox, if it is disabled, it cannot be selected or unselected.

  3. value: this attribute defines string values, and specifies the text to be displayed by default within the input. For example, for an email, you can display a predefined value inside the email field.

Interaction with HTML elements from Selenium

The Selenium library allows interaction with the HTML elements, by means of the following methods implemented within its' classes:

  1. click() - click on an element. The elements you can click on are: buttons, radio buttons, checkboxes. You can also click inside of input fields that allow for typing text (like email, password, regular text input fields). The usage is

  2. sendKeys() - typing something into a field. The actual usage is: element.sendKeys("The text to be typed"). You can type into fields that allow for this type of action (like email, password, regular text input fields). You cannot type inside buttons of any sort (radio/checkbox/regular ones).

  3. getAttribute() - retrieve the specified attribute for any element. The usage is : element.getAttribute(attribute). For example, by applying this method to an element that has a 'class' attribute, this method will return the value of the attribute (by calling getAttribute("class")).

To be able to use these methods, you will first need to give Selenium the reference to the HTML elements you want to interact with. To understand how to create the elements for Selenium tests, you can read this post: But before that, as prerequisite for that post, you should first read about XPATH and CSS selectors.

Recent Posts

See All
bottom of page