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

xpath expression to check multiple values

这是我的 xpath 表达式:

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

就像我要在上面的表达式中添加 50 个国家,所以它会是一个相当大的 xpath 表达式

有什么方法可以使用 "contains method" 或减小表达式的大小,例如

例如: countries.xpath=//*[local-name()='CountryIdentifier']/text()=[1067,874,994,..]

试试这个解决方案,如果它不起作用,请阅读下文。

//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']

假设你有一百个 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");
                    }

在 XPath 2.0 中你可以写

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

在 1.0 中你可以写

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

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