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

How to use apostrophe in formatted xpath?

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

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

当我在我的代码中使用这个定位器时 -

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

类别=女士运动服

当我试图找到它找不到的元素时。 即使我已经尝试过 Women\'s Gym Clothing 但没有成功。

有人可以推荐一个方法吗?

在 XPath 1.0 中,您可以使用单引号或双引号来分隔字符串文字,并且可以使用其他类型的引号在字符串中表示自身。你不能有一个包含单引号和双引号的字符串文字,但你可以使用 concat() 来绕过这个限制:

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

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

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

属性 文件中的定位器:

another.header=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();

下面的也很好用

属性 文件中的定位器:

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

Java代码:

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

或者 属性 文件中的定位器:

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

Java代码:

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