Google+

344. XPath Optimization Strategy One - Use the id attribute if available for the element but not used in XPath Statement







We have to use id attributes in the XPath statements if the element nodes used in the XPath statements have the id attribute available. 

Lets implement This:

2.  In 'Firepath' tab of 'FireBug' options, enter //form  XPath Statement into the XPath field and click on 'Eval' Button as shown below:

3. Ensure that the form element is high lighted on the page as shown below:


4. View the HTML Source of the identified Form element and observe that the form node has id attribute available as shown below:



5. Even though we are able to locate or high light the form element without using its id attribute value in the XPath statement, we've to optimize our XPath statement by using the id attribute to make the XPath statement least breakable. So lets enter the XPath Statement which includes the id attribute i.e. //form[@id='HTMLFormElements'instead of //form XPath Statement into the XPath field and click on 'Eval' button as shown below:



6. Observe that the new XPath statement //form[@id='HTMLFormElements'is high lighting the Form element on the page as shown below and also now our XPath statement is least breakable as we are using id attribute in it.


So we've implemented our First Strategy to optimize the XPath Statements. Lets go through the second strategy to optimize the XPath Statements in next post.






Please comment below to feedback or ask questions.

XPath Optimization Strategy Two - Use the combination of attributes to make the XPath more specific will be explained in the next post.




No comments: