How to use apostrophe in formatted xpath?

喜夏-厌秋 提交于 2020-01-11 11:49:02

问题


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 ?


回答1:


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";
...



回答2:


In XPath 1.0 you can use either single quotes or double quotes to delimit a string literal, and you can use the other kinds of quotes to represent itself within the string. You can't have a string literal containing both single and double quotes, but you can use concat() to get around this limitation:

concat('He said: "', "I won't", '"')

The situation is complicated if the XPath expression appears within a host language that imposes its own constraints; in that case any quotes within the XPath expression must be escaped using host language conventions, for example \" in Java, " in XML.



来源:https://stackoverflow.com/questions/51131453/how-to-use-apostrophe-in-formatted-xpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!