1

Now I have method, which will return me xpath value i.e.

public String xpathValue (String countryName){
return "//*[label="+countryName+"]"
}

And if I call it with Nigeria it will return me value like this:

//*[@label=Nigeria]

But I want value surrounded with double quotes, like this:

//*[@label="Nigeria"]

How can I achieve this?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

1

A valid xpath is:

//*[@label='Nigeria']

and to generate the above, you can use the following line of code:

return "//*[@label='"+countryName+"']";

and to generate:

//*[@label="Nigeria"]

You can use the following line of code:

return "//*[@label=\""+countryName+"\"]";
//                 ^the above backslash indicates that the following character i.e. " is printable

POC

Code:

public class XPathStringDemo {

    public static void main(String[] args) {

        XPathStringDemo obj = new XPathStringDemo();
        System.out.println(obj.xpathValue("Nigeria"));

    }
    public String xpathValue (String countryName){
        return "//*[@label=\""+countryName+"\"]";
        }

}

Output:

//*[@label="Nigeria"]
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352