2

I'm using selenium webdriver to get some text on my webpage using xpath.

This is the code

<a class="ng-binding" data-toggle="tab" href="#tabCreatedByMe">
                            Created By Me                              
<span class="badge ng-binding">3</span>
</a>

I need to get the number '3'. this number is changing everytime

I made this code but it does not return anything

public String getAmountSubtab1() throws InterruptedException{
    WebElement s =  driver.findElement(By.xpath("//*[@class='badge ng-binding']"));
    return s.getText(); }

Suggestions?

3
  • 1
    You mean s.getText returns nothing? If you change it to a .findElements, and check the size of the collection that is returned, how many elements does it have? What if you make your XPath more targetted? //span[@class='badge ng-binding'] Commented Sep 24, 2013 at 21:49
  • When you say it returns nothing, does that mean you get null? Or do you see any NoSuchElementExceptions? Also, use of Xpath is not advisable. I would suggest you to use CSS selectors. Commented Sep 24, 2013 at 22:04
  • Are you using IE? For that case, as Arran said, driver.findElement(...) does not work properly. Commented Feb 14, 2014 at 9:48

1 Answer 1

1

Are you sure you have only one span with the class badge ng-binding It might be that you might have another span before this with the same class name. Advised not to use class name when identifying an element. Use this xpath. Should work.

//a[contains(text(), 'Created By Me')]/span
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.