<a href="https://www.xyz.com/Vertical-Striped-short-Slve-pant-with-Chest-Pocket-dsdjj.html">Vertical-Striped-short-Slve-pant-with-Chest-Pocket-dsdjj
<br><b>Select Size:</b>&nbsp;S</a>

I am trying to fetch the text of anchor tag, but I am getting the text with br tag also.

I am using getText() method of selenium API to fetch the text

Please help me to do solve

Note : I am expecting to get "Vertical-Striped-short-Slve-pant-with-Chest-Pocket-dsdjj"

Getting text : "Vertical-Striped-short-Slve-pant-with-Chest-Pocket-dsdjj Select Size:S"

3 Answers 3


The getText() method returns all the text within a HTML element. In this case all the text between the start <a> and the end </a>. This includes other tags and their contents.

Try to split the string on white spaces and use the first word found.

String value = getText();
String[] words = value.split("\\s+");
String result = words[0];

Now result should contain the expected string.

Also see this question: https://stackoverflow.com/questions/7899525/how-to-split-a-string-by-space

  • Hello @Niels, Thanks for your answer. I found the solution in the same you have provided but actually I want to know is there any way to fetch the text only with in the <a> should not be included text with <b>.Because I am facing this issue regularly in my project.So that I have posted this question.Please let me know is there any other way for this or not ?. If no then I will mark this answer as CORRECT
    – QAMember
    Commented Feb 1, 2016 at 5:18
  • Not that i know of, in your case the <b>old is just part of the link text, but then partial bold. Seems logical to return the full text of the <a>nchor. I would create a wrapper function getWord(element, int) which returns the specified word as a string. Commented Feb 1, 2016 at 6:51

Everything enclosed within the <a> </a> tags is considered as part of the link text.

So if you use the getText() method of Selenium, you will get the whole text.

If you want a part of the text, you should try to shed the unwanted text.

Get the text and store it in a String variable. Trim the unwanted text from that string and you will have the remaining text as you desire.

This link: "http://www.tutorialspoint.com/java/java_string_trim.htm" might help you with the trimming of string.

Happy Testing :)


you should use Java String Substring and not string trim,trim removes only trailing and leading spaces

  • 1
    Komal, perhaps you could add an example?
    – corsiKa
    Commented Jan 31, 2016 at 19:33
  • Komal, your answer is technically correct, but without an example showing the OP how it works it doesn't add anything to the other answers. As @corsiKa suggests, if you add an example you will improve your answer.
    – Kate Paulk
    Commented Feb 1, 2016 at 12:38

Your Answer

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

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