Anyone can you please explain this xpath please. why we use '"+filterBy+"' rather than "+filterBy +" in this code segment. (I just simplify the code.this not an original one.)

String filterBy = "Hio";
System.out.println("//div[@class='filter-option-control-music']/div/label/span[text()='"+ filterBy + "']");

Let's split the elements of your string. You have a string, plus a variable, plus another string:

  • string1://div[@class='filter-option-control-music']/div/label/span[text()='
  • variable: filterBy, with value Hio, as string
  • string2:']

In programming, strings are stored between double quotes usually, so string1 and string1 become:

  • string1 = "//div[@class='filter-option-control-music']/div/label/span[text()='"
  • string2 = "']"

So, string1 + string2 = "//div[@class='filter-option-control-music']/div/label/span[text()='']"

Now, if we want to also add the filterBy variable, we have:

string1 + variable + string2:"//div[@class='filter-option-control-music']/div/label/span[text()='" + filterBy + "']",

therefore, our new string will be:


We use '"+filterBy+"' rather than "+filterBy +", because ' and ' quotes are from text()=' '.

If we would use "+filterBy +" we would have this string:


which is an incorrect xpath, because strings are always between double or single quotes.

More information on strings manipulation can be found here: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Strings

| improve this answer | |
  • this one is more appreciated and expected.Thanks – Aravin Apr 11 '18 at 10:48

It's not

'"+filterBy+"' rather than "+filterBy +"

It's different types of quotation marks used to build a string with quotations inside it. The result is


Two '-s around Hio are the '" and "'.

Here you can read a bit more - Using quotation marks inside quotation marks.

| improve this answer | |

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.