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