Wednesday, January 8, 2014

Contains() and starts-with() function in Xpath

Contains() and starts-with() function in Xpath




Here  as we can see we want to search Google Search just by writing its xpath in console
So to find the Google Search button we have to write xpath like this
//span[@id='gbqfsa']
Once we hit enter it would bring
[
  1. gbqfsa">​Google Search​​
],
It shows that xpath for Google Search Button is correctly written
Now suppose we want to search Google Search button if we are just familiar that id attributes start with gbqfs
then we have to use function starts-with like this
//span[starts-with(@id,'gbqfs')]
and once when we hit enter on console it would reflect two button one is Google Searchand Second one is I’m Feeling Lucky
[
  1. gbqfsa">​Google Search​​
,
  1. ​I'm Feeling Lucky​
]
So to find out the Google Search uniquely we need to complete id attribute to gbqfsa
“//span[starts-with(@id,'gbqfsa')]
and hit to enter and now it would reflect only
[
  1. ​Google Search​
],
It proves that we have written right xpath for Google Search
In the same fashion we can use Contains function to find the Google Search button like this
here I have taken fsa from gbqfsa
//span[contains(@id,'fsa')]  hit enter and hopefully it will return 
[
  1. ​Google Search​
],
if there are multiple attributes then we can use:
//span[contains(@id,'fsa') and contains(@class, 'xyz')]  hit enter and hopefully it will return



No comments:

Post a Comment