Handling Dynamic objects using Xpath in Selenium Webdriver

Automation using selenium is a great experience. It provides many ways to identify an object or element on the web page.
But sometime we face the problems of identifying the objects on a page which have same attributes. When we get more than one element which are same in attribute and name like multiple check boxes with same name and same id. More than one button having


same name and ids. There are no way to distinguish those element. In this case we have problem to instruct selenium to identify a particular.
I am giving you a simple example . In the below html source there are 6 checkboxes are there having same type and same name.
It is really tough to select third or fifth.
input type='checkbox' name='chk' first
input type='checkbox' name='chk' second
input type='checkbox' name='chk' third
input type='checkbox' name='chk' forth
input type='checkbox' name='chk' fifth
input type='checkbox' name='chk' sixth
There are some function we can use in Xpath to identify the abject in above cases. An XPath expression can return one of four basic XPath data types:

  • String
  • Number
  • Boolean
  • Node-set
XPath Type : Functions
Node set : last(), position(), count(), id(), local-name(), namespace-uri(), name()
String : string(), concat(), starts-with(), contains(), substring-before(), substring-after(), substring(), string-length(), normalize-space(), translate()
Boolean : boolean(), not(), true(), false(), lang()
Number : number(), sum(), floor(), ceiling(), round()
 

I will show you how we can use some of these above functions in xpath to identify the objects.


Node Set : last()

In the above html file there are six checkboxes and all are having same attributes (same type and name)

How we can select the last checkbox based on the position. We can use last() function to identify the last object among all similar objects.
Below code will check or uncheck the last checkbox.
selenium.click("xpath=(//input[@type='checkbox'])[last()]");
How we can select the second last checkbox and third last checkbox. We can use last()- function to identify the last object among all similar objects.

Below code will check or uncheck the second last checkbox and third last checkbox respectively.

selenium.click("xpath=(//input[@type='submit'])[last()-1]");
selenium.click("xpath=(//input[@type='submit'])[last()-2]");
Node Set : position()

If you want to select any object based on their position using xpath then you can use position() function in xpath.

You want to select second checkbox and forth checkbox then use below command
selenium.click("xpath=(//input[@type='checkbox'])[position()=2]");
selenium.click("xpath=(//input[@type='checkbox'])[position()=4]");
above code will select second and forth checkbox respectively.


String : starts-with()

Many web sites create dynamic element on their web pages where Ids of the elements gets generated dynamically.

Each time id gets generated differently. So to handle this situation we use some JavaScript functions.
XPath: //button[starts-with(@id, 'continue-')]
Sometimes an element gets identified by a value that could be surrounded by other text, then contains function can be used.

To demonstrate, the element can be located based on the ‘suggest’ class without having
to couple it with the ‘top’ and ‘business’ classes using the following
XPath: //input[contains(@class, 'suggest')].

List of xpath functions:
  • //tagname[@attibuteType = ‘attributevalue’]
  • //tagname[text()= ‘textvalue’]
  • //*[@attribute =’attributevalue’]
  • //*[text() = ‘textvalue’]
  • //tagname[starts-with(@attributetype, ‘attributeValue’)]
  • //tagname[starts-with(text(), ‘textvalue’)]
  • //tagname[contains(@attributetype, ‘attributeValue’)]
  • //tagname[contains(text(), ‘textvalue’)]
  • //tagname[@attibuteType1 = ‘attributevalue1’ and @attibuteType2 = ‘attributevalue2’]
  • //tagname[@attibuteType1 = ‘attributevalue1’ or @attibuteType2 = ‘attributevalue2’]
  • //tagname[@attibuteType = ‘attributevalue’]/..
  • //tagname[@attibuteType = ‘attributevalue’]/parent::div
  • (//tagname[@attibuteType = ‘attributevalue’])[1]
  • //tagname[@attibuteType = ‘attributevalue’]/following-sibling::tagname[index]
  • //tagname[@attibuteType = ‘attributevalue’]/preceding-sibling::tagname[index]
  • //tagname[@attibuteType = ‘attributevalue’]/childtagname[index]
  • (//*[contains(@id,"persona")][not(contains(@type,'hidden'))])[3]

Find video :