代码之家  ›  专栏  ›  技术社区  ›  NarendraR TheSociety

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

  •  1
  • NarendraR TheSociety  · 技术社区  · 8 年前

    我把定位器放在属性文件中,比如:

    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 但没有成功。

    有人能推荐一种方法吗?

    2 回复  |  直到 8 年前
        1
  •  1
  •   user861594    8 年前

    以下是我的不同工作方式:

    属性文件中的定位器:

    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";
    ...
    
        2
  •  3
  •   Michael Kay    8 年前

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

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

    如果xpath表达式出现在具有自身约束的宿主语言中,则情况会很复杂;在这种情况下,xpath表达式中的任何引号都必须使用宿主语言约定进行转义,例如 \" 在爪哇, " 在XML中。

    推荐文章