Reputation: 13
This is my xpath expression:
countries.xpath=//[local-name()='CountryIdentifier']/text()='1067' or //[local-name()='CountryIdentifier']/text()='874' or //*[local-name()='CountryIdentifier']/text()='994' .............
like i have 50 countries to add in the above expression so it will be quite big xpath expression
Is there any way to use "contains method" or to reduce the size of the expression like
eg: countries.xpath=//*[local-name()='CountryIdentifier']/text()=[1067,874,994,..]
Upvotes: 1
Views: 1126
Reputation: 163322
In XPath 2.0 you can write
//*:CountryIdentifier[.=(1067,874,994,..)]
In 1.0 you can write
//*[local-name()='CountryIdentifier'][.=1067 or .=874 or .=994 ...]
Try to avoid using text(): you don't want the text nodes, you want the string value of the element. Using text() makes your expression less robust, eg. it fails if there are comments or if the elements you are selecting have internal markup. And in an expression like this, it makes your code much more long-winded.
Upvotes: 1
Reputation: 454
Lets assume you have hundred Country Identifier
, At that time you cant use //CountryIdentifier[text()='17' or text()='46' or text()='94'or....]
for hundred values.
Follow the below steps..
When you want to check for large data, you can you following code
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");
}
First I'm saving all Country Identifier
text in Set<String>
In this if statement you can pass your own text and verify whether it is present in the retrieved set.
if(a.equalsIgnoreCase("Pass ur text here to verify whether it is present in the set"))
System.out.println("present in UI");
}
Upvotes: 0
Reputation: 5419
Try this for your solution and if it does not work read below.
//CountryIdentifier[text()='1067' or text()='874' or text()='994']
Let's assume that your XML looks like this:
<root>
<Countries>
<CountryIdentifier id="2">12</actor>
<CountryIdentifier id="3">13</actor>
<CountryIdentifier id="4">14</actor>
</Countries>
</root>
In order to get specific countries:
by ID:
/root/Countries/CountryIdentifier[@id='2' or @id='4']
by node value
/root/Countries/CountryIdentifier[text()='12' or text()='14']
Upvotes: 0