Hi Danish, for handling complex and dynamic web elements, selenium provides some methods which can be used with Xpath. Following are the Xpath functions which are widely used:
1) Basic Xpath: This function selects nodes or list of nodes on the basis of attributes such as name, class name, id, etc. For eg. Xpath=//input[@id='id1'] or Xpath=//*[@class='class1'
2) Contains(): It is a method that is used when the value of any attribute changes dynamically e.g. login information. It can locate a web element with the available partial text. For eg. Xpath=.//* [contains (@name, ‘button’)] or Xpath=//*[contains(text (),’sector’)]
3) Using OR & AND: In the case of the OR expression, we use two conditions. Here either 1st condition OR 2nd condition should be true. It is applied when one condition is true or both. It means that at least one condition should be true to find a web element. For eg. Xpath=//*[@type=’submit’ OR @name=’buttonSubmit’]
In the case of the AND expression, we use two conditions. Here both conditions should be true to locate a web element. It will fail to locate an element if any of the conditions are false. For eg. Xpath=//input[@type=’submit’ AND @name=’buttonEnter’]
4) Start-with function: This function is used to find a web element whose value of attribute changes on the refresh or on any dynamic operation on the web page. In this expression, we match the starting text of the attribute that is used to locate an element whose attribute has changed dynamically. For eg. On a web page ID of a particular element changes dynamically such as ‘id1’, ‘id2’, ‘id3’, etc. but the text remains the same. So the Xpath for this element would be: Xpath=//label[starts-with(@id, ‘message’)]
5) Text(): This expression is used with the text function to locate an element with exact text. Following are the examples of text expression. For eg. Xpath=//td