Need xpath for the below script where i need to store the number 3:

<div class="col-xs-4">
 <div class="col-xs-8">
  <small class="stat-label">
   <a style="color:#fff;" href="www.mywebsite.com">Customers</a>

The problem is the same code is used in three other places also. The only change in other places are the href url and the name (eg: Customers)

I tried this:

int customCount = driver.findElements(By.xpath("//div[@class='col-xs-8']/h1")).size(); [But firepath highlights in other places also and gets some other value]

and this:

//div[@class='col-xs-8']/small [Again highlights in other places]

Is there anyway to get the number 3?


Don't use Xpath. Locate element by tag.


WebElement header1 = driver.findElement(By.tagName("h1"));

  • How? Can you give an example? – Deepak Nath Mar 17 '16 at 17:53
  • The problem with that is in the page there are other h1 tags also. The above same function is used around 3 times. – Deepak Nath Mar 17 '16 at 19:26
  • 1
    findElements() (note plural) will give you a list of all h1 elements. Then you can loop over the collection, check other attributes and find the h1 you want. – Peter M. - stands for Monica Mar 17 '16 at 20:03

Found a way to locate locate and display guys:

First i had to locate it properly

driver.findElement(By.xpath("//div[@class='col-xs-8' and ./small/a/text()='Customers']/h1"))

Now i had to store the number 3 so that i can display or can compare it. So i used WebElement.

WebElement e = driver.findElement(By.xpath("//div[@class='col-xs-8' and ./small/a/text()='Customers']/h1"));

Now the problem was i needed an integer and i stored the value as a string. So i had to convert the string to and integer

int customCount = Integer.parseInt(e.getText());

And the above thing works perfectly. The customCount has value 3

Thanks everyone for helping me out.


Given that the same code is used multiple times in the page, this is a case where I would go back to the developer of the page and request/insist that an id be added to the header tag. Adding the id will take very little development time, adds negligible risk to the product, but will save the tester a lot of time head scratching, testing xpath code, and maintaining the test(s) long-term.

The major problem with relying on xpath is that it is dependent on the path you give it. What if tomorrow's task is to add another h1 block to the same div found by your xpath search? Now you have two possible elements instead of the one you anticipated. Since web code tends to change quickly, having a dedicated identifier that is unlikely to change (unless the purpose of that specific element changes) will make your tests easier to maintain and less brittle over time.


Sometimes I find the values inside the h tags aren't picked up by getText() - I found that using getAttribute("innerHTML") works in those cases. For example, I needed to extract the word "People" and there are many h4 tags on the page.

<div id="stakeholders" role="tabpanel">

The code below did NOT work, getText() brought back an empty string:

String people = getCurrentDriver().findElement(By.xpath("//*[@id=\"stakeholders\"]/h4")).getText();

The code below DID work.

String people = getCurrentDriver().findElement(By.xpath("//*[@id=\"stakeholders\"]/h4")).getAttribute("innerHTML");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.