How to use Contains() and starts-with() function in Xpath

Contains() and starts-with() function in XPath is used when we are familiar with the pattern of dynamically changing attribute’s value of an element on HTML pages. This not only works with dynamic values of any of the HTML attributes but it also works when we want to write XPath on the basis of a partial pattern of any of attribute. Here I am trying to explain how Contains() and starts-with() function are used in writing XPath.

Here I am adding one Snap to show how it works

Xpath In Selenium

Here as we can see, we want to search Google Search Button just by writing its XPath in the console of developer tools used in Chrome. To know another way to validate your XPath in chrome browser, I would suggest you read my post

Inspecting Elements for writing XPath, CSS Selector in Chrome

So to find the Google Search button we have to write xpath like this

$x(“//input[@name=’btnK’]”)

Once we hit enter it would bring

[<input value=​”Google Search” aria-label=​”Google Search” name=​”btnK” type=​”submit” jsaction=​”sf.chk”>​]

It shows that xpath for Google Search Button is correctly written

Also Read: Arrays in Java and its implementation in WebDriver

Recommended Paid Courses to Lean Selenium and Locators:

Now suppose we want to search Google Search button if we are just familiar that value of name attributes is starting with  “btn”

then we have to use function starts-with like this

$x(“//input[starts-with(@name,’btn’)]”)

and once when we hit enter on console it would reflect two button one is Google Search and Second one is I’m Feeling Lucky

[<input value=”Google Search” aria-label=”Google Search” name=”btnK” type=”submit” jsaction=”sf.chk”>, <input value=​”I’m Feeling Lucky” aria-label=​”I’m Feeling Lucky” name=​”btnI” type=​”submit” jsaction=​”sf.lck”>​]

So we can see that all the elements those are having name attributes matching with “btn” is coming on screen. So to find Google Search button uniquely we need to use the complete value of name attribute

$x(“//input[starts-with(@name,’btnK’)]”) 

put above in console of chrome’s developer tool and hit enter and then it will return

[<input value=​"Google Search" aria-label=​"Google Search" name=​"btnK" type=​"submit" jsaction=​"sf.chk">​]

It proves that we have written right xpath for Google Search

In the same fashion we have  Contains function that finds all the element matching with string pattern. So if we know that all button on any screen is going to have one pattern of value against its any of the attribute like id, name, class or any of the attribute in html code then we can use contains because contains function find all the element on webpage with matching pattern.
Also Read: Reading e-mail using Javamail api
So lets take example like all buttons on Google.com is going to have one pattern for its name attribute value that is “tn”

For this example we are taking pattern that is common in name attributes value in html code of Google Search button and I’m feeling lucky button and that is “tn” since this string is common in value of name attribute in both line of html code

1- GOOGLE SEARCH BUTTON

<input value="Google Search" aria-label="Google Search" name="btnK" type="submit" jsaction="sf.chk">

2- I'm feeling lucky

<input value="I'm Feeling Lucky" aria-label="I'm Feeling Lucky" name="btnI" type="submit" jsaction="sf.lck">

So we can write xpath using Contains function like this and if we put it in console of chrome’s developer tool and hit enter

$x(“//input[contains(@name,’tn’)]”)

[<input value=​"Google Search" aria-label=​"Google Search" name=​"btnK" type=​"submit" jsaction=​"sf.chk">​, <input value=​"I'm Feeling Lucky" aria-label=​"I'm Feeling Lucky" name=​"btnI" type=​"submit" jsaction=​"sf.lck">​]

above line of code would appear on screen.
Also Read: Email Verification from Gmail Account in Selenium Webdriver (Java)

One more thing contains method always match pattern in values of attribute and if it find it any where in value then it fetches the element on screen and let the Selenium WebDriver code to work on these elements.

Dwarika Dhish Mishra

My name is Dwarika Dhish Mishra, its just my name and I am trying to bring the worth of my name in to actions and wants to be the solution not the problem. I believe in spreading knowledge and happiness. More over I am fun loving person and like travelling a lot. By nature I am a tester and a solution maker. I believe in the tag line of http://ted.org “Idea worth spreading” . For the same, I have created this blog to bring more and more learning to tester fraternity through day to day learning in professional and personal life. All contents are the part of my learning and so are available for all..So please spread the contents as much as you can at your end so that it could reach to every needful people in testing fraternity. I am pretty happy that more and more people are showing interest to become the part your Abode QA blog and I think this is good sign for us all because more and more content would be before you to read and to cherish. You may write or call me at my Email id: dwarika1987@gmail.com Cell: 9999978609

You may also like...

14 Responses

  1. rahul says:

    Thanks Dwarika, was searching for the usage of these Xpath functions and after 1 hr, search finally ends at this page.

    Thanks again for sharing 🙂

    • its my pleasure that this post has helped you

      Thank you and be in touch with update

      • Vinod says:

        I am facing one problem while navigating. So I need your help. Please send me a command by which I can open another browser while my current browser remains open.

        “how to navigate from one browser to another in selenium.”

  2. bharath says:

    Hi,
    please could you help me .
    i am trying to automate make my trip .
    i want to select a source for the flight ,
    where as the html tags are below

    New Delhi, India (DEL)

    please let me know how to access the new delhi .
    below is the code i have used
    WebElement ele1=driver.findElement(By.xpath(“.//*[@id=’ui-id-1′]”));

    List Links=ele1.findElements(By.xpath(“//li”));
    String[] linkTexts = new String[Links.size()];
    int j = 0;
    int lsize=Links.size();

    for (int i=2;i<Links.size()-1;i++){
    System.out.println(Links.size());
    String s1=".//*[@id='ui-id-1']/li[";
    String s2="]";
    String s3=s1+i+s2;
    WebElement ele2;

    ele2=driver.findElement(By.xpath(s3));

    WebElement Sublinks=ele2.findElement(By.tagName("a"));
    System.out.println(Sublinks.getText());

  3. meer says:

    Why can’t I see your screen shots in my browser..Please let me know..? Thanks in advance!

  4. Ram says:

    how to validate a excel profile in android using appium and tell the suitable one for ios also

    best android training in selenium with hands on get it selenium training centers in chennai

  5. Pawan Purohit says:

    Thank you for sharing this with us.

  6. deepti pandey says:

    0
    down vote
    Can any one tell why xath is not working using contains and starts with-

    id=”u_0_3″

    //*[starts-with(@id,’u-‘)]- startswith

    //*[contains(@id,’u-‘)]- contains

    can anyone tell correctxpath of id after u is changing randomly and all other attibutes like-classname,css are same. Please provide the solution.

    • Sorry for delay in replying.

      Since you id value is u_0_3
      But you are using u- in contains and starts-with function
      So try this one
      //*[starts-with(@id,’u_‘)]

      and it will work for you.

  7. Vinod says:

    I am facing one problem while navigating. So I need your help. Please send me a command by which I can open another browser while my current browser remains open.

    “how to navigate from one browser to another in selenium.”

  8. anand says:

    nice

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.