用于检查多个值的xpath表达式

时间:2015-10-07 07:45:20

标签: xml xpath expression contains

这是我的xpath表达式:

countries.xpath = // [本地名称()=' CountryIdentifier'] /文本()=' 1067'或// [local-name()=' CountryIdentifier'] / text()=' 874'或// * [local-name()=' CountryIdentifier'] / text()=' 994' .............

就像我有50个国家要添加上面的表达式所以它将是相当大的xpath表达式

有没有办法使用"包含方法"或者减少表达式的大小,如

例如:  countries.xpath = // * [本地名称()=' CountryIdentifier'] /文本()= [1067,874,994,..]

3 个答案:

答案 0 :(得分:1)

在XPath 2.0中,您可以编写

//*:CountryIdentifier[.=(1067,874,994,..)]

在1.0中你可以写

//*[local-name()='CountryIdentifier'][.=1067 or .=874 or .=994 ...]

尽量避免使用text():你不想要文本节点,你想要元素的字符串值。使用text()会使表达式不那么健壮,例如。如果有注释或者您选择的元素具有内部标记,则会失败。在这样的表达式中,它使你的代码更加冗长。

答案 1 :(得分:0)

请尝试使用此解决方案,如果不起作用,请阅读以下内容。

//CountryIdentifier[text()='1067' or text()='874' or text()='994']

我们假设您的XML看起来像这样:

<root>
    <Countries>
        <CountryIdentifier id="2">12</actor>
        <CountryIdentifier id="3">13</actor>
        <CountryIdentifier id="4">14</actor>
    </Countries>
</root>

为了获得特定国家/地区:

ID:

/root/Countries/CountryIdentifier[@id='2' or @id='4']

按节点值

/root/Countries/CountryIdentifier[text()='12' or text()='14']

答案 2 :(得分:0)

假设您有一百Country Identifier,那时您无法使用//CountryIdentifier[text()='17' or text()='46' or text()='94'or....]一百个值。

按照以下步骤操作..

如果您想检查大数据,可以使用以下代码

 Set<String> set = new HashSet<String>();
                List<WebElement> ab =driver.findElements(By.xpath("//*[local-name()='CountryIdentifier']"));
                for(int i=0;i<ab.size();i++)
                {
                    set.add(ab.get(i).getText());   
                }
                System.out.println(set);
                for(String a:set)
                {
                    if(a.equalsIgnoreCase("Pass ur text here to verify whether it is present in the set"))
                        System.out.println("present in UI");
                }

首先我保存Country Identifier

中的所有Set<String>文字

在此if语句中,您可以传递自己的文本并验证它是否存在于检索到的集合中。

if(a.equalsIgnoreCase("Pass ur text here to verify whether it is present in the set"))
                            System.out.println("present in UI");
                    }