There are several different methods for finding elements on a page.
Deciding which one to use depends on a lot of different factors.
There is no correct answer, other than the one that works for you.
In many cases,
several different locators could all be used to accomplish the same task.
There are eight main methods in total.
Let's have a look at these methods one by one.
Locating elements by name.
It is standard practice to define unique IDs for web elements in HTML code.
However, there may be cases when these unique identifiers are not present.
Instead, the names are there.
So locating elements by name is used when you know
the name attribute of an element.
Locating elements by ID.
We use this method when the ID attribute for the element is available.
It is, in fact,
the most reliable and fastest way to locate a
particular web element on an HTML page.
And ID will always be unique for any object on a web page.
So,
we should prefer using ID attribute for locating the
elements or other available options.
Locating elements by link text.
This method is used for selecting hyperlinks from a web page.
Locating elements by partial link text.
For locating the element by using the link text method,
we need to provide the complete link text.
However,
the partial link text method enables us to select a hyperlink
by giving only a part of the link text.
Locating elements by CSS Selector.
This method is used when you want to locate an element by CSS Selector syntax.
A CSS Selector is a combination of an element selector and a value
which identifies the web element within a web page.
They are string representations of HTML tags, attributes, ID, and class.
Locating elements by tagname.
As the title says,
this method allows you to find a web element by specifying the tagname.
Locating elements by classname.
This method is used when you want to locate an element by class attribute name.
Locating elements by XPath.
Let's take this one separately, as there is more to explain here.
XPath is a language used for locating nodes in an XML document.
Through XHTML,
HTML can be an implementation of XML and thus Selenium
users can leverage this powerful language to target
elements in their web applications.
XPath goes beyond, as well as supports,
the simple methods of locating by ID or name attributes,
and opens up all sorts of new possibilities.
One of the main reasons for using XPath is when you don't have a suitable
ID or name attribute for the element you wish to locate.
You can use XPath to either locate the element in absolute terms or relative
to an element that does not have an ID or name attribute.
Absolute XPath determines the location of an object from the route.
However, using this is not advised.
Why?
It's because even if we make a slight change in the web page code,
absolute XPath will change,
and the web driver may not be able to locate the element with the old one.
In case of relative XPath,
we try to locate a nearby element for which an ID or name attribute is given.
Now,
we can calculate XPath of the target element relative to this nearby element.
The chances of this XPath changing is very less,
thus making our test more robust.
XPath locators can also be used to specify elements via
attributes other than ID and name.
The basic format of XPath is given below.
Let's break that down.
The first part with the double forward slash depicts
the selection of the current node.
The tagname part is how tag names, such as input, dev, etc.
are selected.
The at part signals the selection of the attribute.
Attribute is the attribute name and the value is the value of the attribute.
All these ways to locate elements will be shown in more detail
with code in the demo later on in the module.
All these methods of locating elements can be performed using two techniques,
find element and find elements.
Find element command is used to uniquely identify a
single web element within the web page,
whereas find elements is used to uniquely identify the
list of web elements within the web page.
Let's look at the main differences between the two.
Find element returns the first element if there are multiple
web elements found with the same locator.
Find elements returns a list of web elements.
Find elements throws exception NoSuchElementException if there
are no elements matching the locator strategy.
Find elements returns an empty list if there are no web
elements matching the locator strategy.
In find elements, each web element is indexed starting from 0,
like an array,
which is obviously not applicable in find element
since it returns a single element.