Working with dynamic xpath in selenium webdriver



Working With Dynamic Xpath

To click on to 3 different drop down  in same page:
HTML DOM Select Object Structure:-


Now in regular practice we have to create 3 different xpaths to move to each drop down .
As for example:
1.       //select[@id='day']
2.       //select[@id='month']
3.       //select[@id='year']

To reduce count of locator what we can do here is , we have to find the similarities in these 3 xpaths .
Similarity is //select[@id=’ ’]
Difference is the id values .
What we can do to make these all 3 xpaths as one:
String fbdefaultPageDropdown="//select[@id='%s']";
One method which is going to take care of all these 3 xpaths and to click on to the drop down:

Suppose below method is a generic method to click on to facebook drop down which is in PageWebDriverClient class:-





In my Test Class I will be calling this method and simply pass the id :
clickFacebookDorpDown(“year”):
It will internally call clickFacebookDorpDown(String id) method , generate xpath at run time and click on to the year drop down.
Similarly for month and year.

Advantage of dynamic xpaths::
So what we have achieved here is one locator and one method is taking care of all the drop downs.
At run time an xpath is constructed and desired actions are taken on those xpaths.
Disadvantage of constant xpaths:
If at run time we would have not constructed dynamic xpath then for each drop down we need to create different sets of locators and duplicate methods to take care of the clicking activity.



Working with dynamic xpath on Text Boxes
In Facebook Login Screen we have 4 Text boxes suppose and I want to pass values to these text boxes.
In general what we do is for 4 different text boxes we create 4 different locators and pass values using sendKeys() method .
4 Different locators for these text boxes are as under:
//input[@aria-label='First name']
//input[@aria-label='Surname']
//input[@aria-label='Mobile number or email address']
//input[@aria-label='New password']
Or, we can search web element directly via id/class name for uniqueness.

Below methods will help us to send values to 4 text boxes.




Suppose there are 100 text boxes then we need to write 100 different locators and methods for each fields to send values to the fields.

To overcome this problem we can take help of dynamic xpath.
How to achieve this:-
Again we have to find similarities between these 4 fields locators.
Similarity is:-
//input[@aria-label=’ ’]
 Difference is the name of the text boxes .
What we can do to make these all 4 xpaths as one.
       String textBoxXpath="//input[@aria-label=’%s’]";

And create a single method which will generate text boxes unique xpaths at run time and then we can perform desired actions on the webelement.



In my Test Class I will be calling this method and simply pass the fieldName and value to be passed and rest of the stuff will be taken at run time.
sendValuesToTextBoxFields(“First name”,”xyz”);

Radio Button
Non Generic xpath:
//label[text()='Male']
//label[text()=' Female ']

We can parameterize text Male/Female with sex and at run time we will pass the required sex and then we can perform desired action on it.

                                                     Drop Down options selection:
Here we can pass 2 parameters:
1.       On which drop down we need to perform operation
2.       Which value we need to select.
And this will be generic to all the remaining drop downs.




Similarly for multiple buttons , hover text etc.
NOTE: To generate generic xpaths we need to first identify the similarities between similar type of web elements and will be purely based on our judgment.











1 comment:

. said...

Thank you, You have spent good time I guess to write this kind of good articles, there is link for xpath, please do read it.relative XPATH in Selenium webdriver

About Me

My photo
Pune, Maharastra, India
You can reach me out at : jimmiamrit@gmail.com