Java 如何在格式化xpath中使用撇号?

Java 如何在格式化xpath中使用撇号?,java,selenium-webdriver,xpath,string-formatting,qaf,Java,Selenium Webdriver,Xpath,String Formatting,Qaf,我已将定位器放置在属性文件中,如: header.navigation.product.link = //div[contains(@class,'grid-')]//li/a[contains(.,'%s')] 当我在代码中使用这个定位器时- String headerproductlink = String.format(ConfigurationManager.getBundle() .getString("header.navigation.category.l

我已将定位器放置在属性文件中,如:

header.navigation.product.link = //div[contains(@class,'grid-')]//li/a[contains(.,'%s')]
当我在代码中使用这个定位器时-

String headerproductlink = String.format(ConfigurationManager.getBundle()
            .getString("header.navigation.category.link"), category)
类别=女式运动服

当我试图找到它找不到的元素时。 甚至我也试过做
女式运动服
,但没有成功


有人能推荐一种方法吗?

在XPath 1.0中,可以使用单引号或双引号来分隔字符串文字,也可以使用其他类型的引号在字符串中表示自身。不能让字符串文字同时包含单引号和双引号,但可以使用concat()绕过此限制:

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

如果XPath表达式出现在施加其自身约束的宿主语言中,情况就复杂了;在这种情况下,XPath表达式中的任何引号都必须使用宿主语言约定进行转义,例如Java中的
\“
,XML中的

在XPath 1.0中,可以使用单引号或双引号来分隔字符串文字,也可以使用其他类型的引号在字符串中表示自身。不能让字符串文字同时包含单引号和双引号,但可以使用concat()绕过此限制:

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

如果XPath表达式出现在施加其自身约束的宿主语言中,情况就复杂了;在这种情况下,XPath表达式中的任何引号都必须使用宿主语言约定进行转义,例如Java中的
\“
,XML中的

以下不同的方法对我有效:

属性文件中的定位器:

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

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();
String t = "st. john\\'s bay - women";
...
String t = "st. john's bay - women";
...
下面也很好

属性文件中的定位器:

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

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();
String t = "st. john\\'s bay - women";
...
String t = "st. john's bay - women";
...
或 属性文件中的定位器:

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

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();
String t = "st. john\\'s bay - women";
...
String t = "st. john's bay - women";
...

以下几种方法对我有效:

属性文件中的定位器:

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

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();
String t = "st. john\\'s bay - women";
...
String t = "st. john's bay - women";
...
下面也很好

属性文件中的定位器:

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

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();
String t = "st. john\\'s bay - women";
...
String t = "st. john's bay - women";
...
或 属性文件中的定位器:

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

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();
String t = "st. john\\'s bay - women";
...
String t = "st. john's bay - women";
...

如果您的类别没有双引号,您可以将
'%s'
更改为
'%s'
。如果你的类别没有双引号,你可以将<代码> '%s' <代码>改为<代码>“%s”< /代码>。一些你可以考虑的事情。