How to handle dynamic element or complex elements in Selenium ?

1. Basic Xpath

Syntax: //tagName[@attribute = 'value']
xPath Example = //input[@id = 'login' ]

We can also use class name link text and other attributes to locate an element with xPath as shown above.

2. Using Contains()

Contains() is a method used in xPath expression. It is very handy when the value of any attribute changes dynamically. The contains feature has an ability to find the element with partial text. For example login filed has id and its ending number keeps changing every time the page is loaded.

Syntax: //tagName[contains(@attribute, 'value')]
xPath Example = //input[contains(@id, 'email')]

3. Using Starts-With()

It is used for finding the web elements whose attribute values gets changed on refresh or by other dynamic operations on the web page.

Syntax: //tagName[starts-with(@attribute, 'value')]
xPath Example = //input[starts-with(@id, 'user')]

4. Using Text()

text() function is a a built in function of Selenium webdriver which is used to locate elements based on text of a web element. This comes really handy when the other attribute values change dynamically with no substantial part of the attribute value that can be used via Starts-with or Contains.

Syntax: 1. //tagName[text(), 'text value']
        2. //tagName[contains(text(), 'value')]
xPath Example = //td[text()='UserID']

5. Using OR & AND operators

In OR expression, two conditions are used, whether 1st condition OR 2nd condition should be true. It is also applicable if any one condition is true or maybe both.

Syntax: //tagName [ xPath1 or xPath2]
xPath Example = //*[@type='submit' or @name='btnReset']

In AND expression, two conditions are used, both conditions should be true to find the element. 

Syntax: //tagName [ xPath1 and xPath2]
xPath Example = //*[@type='submit' and @name='btnLogin']

6. Using  XPath axes methods

XPath axes comes in useful when the web element is dynamic and complex. In this case locating element after traversing through child/sibling or parent will be easy approach. Some of the widely used XPath axes are:

a. Following: This XPath axes helps to locate element following the current node

Xpath Example =//input[@name=’ organization_name’]//following::input[1]

b. Ancestor: The ancestor axis selects all ancestors element (grandparent, parent, etc.)

Xpath Example= //input[@name=’email]//ancestor::div[1]

 c. Child: As the name specified this approach is used when we intent to locate all child elements of the current node. A basic use case of this approach could be when we wish to circulate through all the data in a table through the rows, in this case we can find all the child of a particular table.

Xpath Example = //*[@id='java_technologies']//child::li

d. Parent: Selects the parent of the current node 

Xpath Example =//*[@id='rt-feature']//parent::div

e. Preceding: Select all nodes that come before the current node

Xpath Example =//*[@type='submit']//preceding::input

f. Descendants: This approach is used to locate element via XPath for all the children and sub children of the current node.

Xpath=//*[@id='rt-feature']//descendant::a
Design a site like this with WordPress.com
Get started