·
Locating UI Elements (Web Elements)
Fine,
Now our project setup is ready, So that we can go ahead with creating the
scripts and execution, But before that it is necessary to know, how WebDriver
able to identify the UI elements
Locating
elements in WebDriver can be done on the WebDriver instance itself or on a
WebElement. Each of the language bindings exposes a “Find Element” and “Find
Elements” method.
FindElement : It returns a WebElement
object otherwise it throws an exception
findElements: The latter returns a list of WebElements, it can return
an empty list if no DOM elements match the query.
The
“Find” methods take a locator or query object called “By”. “By”
strategies are listed below.
1. BY
ID : This is the most efficient and preferred way to locate an
element.
Example:
2. By Class Name
“Class” in this case refers to the attribute on the DOM element. Often
in practical use there are many DOM elements with the same class name, thus
finding multiple elements becomes the more practical option over finding the
first element
3. By Tag Name
The DOM Tag Name of the element.
4. By Name
Find the input element with matching name
attribute.
5. By Link Text
Find the link element with matching visible text.
Source:
Ex :
6. By Partial Link Text
Find the link element with partial
matching visible text.
Source:
Example:
7. By CSS
Like the name implies it is a locator
strategy by css
Ex:
8. By XPATH
At a high level, WebDriver uses a
browser’s native XPath capabilities wherever possible. On those browsers that
don’t have native XPath support, we have provided our own implementation. This
can lead to some unexpected behaviour unless you are aware of the differences
in the various XPath engines.
Ex :
No comments:
Post a Comment