0

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 + "']");

2 Answers 2

4

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:

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

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

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

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

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

1
  • this one is more appreciated and expected.Thanks
    – Aravin
    Commented Apr 11, 2018 at 10:48
1

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

//div[@class='filter-option-control-music']/div/label/span[text()='Hio']

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

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

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.