Search code examples
javaselenium-webdriverxpathstring-formattingqaf

How to use apostrophe in formatted xpath?


I have place the locator in properties file like :

header.navigation.product.link = //div[contains(@class,'grid-')]//li/a[contains(.,'%s')]

and while I'm using this locator in my code-

String headerproductlink = String.format(ConfigurationManager.getBundle()
            .getString("header.navigation.category.link"), category)

And category = Women's Gym Clothing

While I'm trying to locate the element it unable to find. even i have tried as Women\'s Gym Clothing but no success.

Can someone please suggest a way ?


Solution

  • Below different ways worked for me:

    Locator in Property file:

    another.header=xpath=//h1[contains(.,"%s")]
    

    Java code:

    String t = "st. john\'s bay - women";
    String header = String.format(getBundle().getString("another.header"), t);
    CommonStep.get("https://www.jcpenney.com/g/st-johns-bay-women/N-bwo3xZ1z0nvauZ1z0nh7w");
    String headerText=ElementFactory.$(header).getText();
    

    Below also worked fine

    Locator in Property file:

    another.header={'locator':'xpath=//h1[contains(.,"%s")]'}
    

    Java code:

    String t = "st. john\\'s bay - women";
    ...
    

    Or Locator in Property file:

    another.header={"locator":"xpath=//h1[contains(.,\\"%s\\")]"}
    

    Java code:

    String t = "st. john's bay - women";
    ...