0

I have a span tag like this:

<div class="row-flex">
  <span class="icon-arrow-down-outline rotate-v"></span>
  <span class="bmi-value ng-binding">22</span>
</div>

I want to get the value 22 of this span tag, so I write java code like this:

WebElement spanValue = webDriver.findElement(By.xpath("//span[@class='bmi-value ng-binding']"));        
String value = spanValue.getText();

but it always returns an empty string. What is missing?

Ragnarsson
  • 1,715
  • 7
  • 41
  • 74
  • It's been a while since I last used an xpath selector but if I recall you can't include a space within the selector. `By.cssSelector(".bmi-value.ng-binding")` might work instead? – Mark Rowlands Nov 13 '15 at 14:50
  • Is it possible that there are multiple elements in the page that can be located with that locator? If that's the case, and if you have control over the HTML, you could add an id to the element you are interested in, like this: `22` – alb-i986 Nov 13 '15 at 14:50

1 Answers1

0

Maybe there are several span elements with the same class on your site, you can try to be more specific like this:

WebElement spanValue = webDriver.findElement(By.xpath("//div[@class='row-flex']/span[@class='bmi-value ng-binding']"));        
String value = spanValue.getText();
drkthng
  • 6,651
  • 7
  • 33
  • 53
  • yeah, just found out there is another HTML part with the same div and span, but it's hidden, so I didn't notice. They are exactly identical, so for now, I just use findElements and get index. Thanks all for your answers – Ragnarsson Nov 13 '15 at 15:02
  • @LouisT working with indexes is not the best approach, since you are prone to changes in the structure of the website, for example if the developers put another of these spans in the code, then you have to fix your queries all the time... – drkthng Nov 13 '15 at 15:10
  • yeah I know, I found a solution, still use findElement and getAttribute("textContent") and it returns the exact value of the visible span – Ragnarsson Nov 13 '15 at 15:28