Take the length of the xpath - xpath

I have a list which I can take their values using the following xpaths:
.//*[#id='LayoutWrapper']/div[6]/div[3]/main/section[3]/div/div/div[1]/xpl-result/div/div[1]/div[1]/div[1]/span
.//*[#id='LayoutWrapper']/div[6]/div[3]/main/section[3]/div/div/div[2]/xpl-result/div/div[1]/div[1]/div[1]/span
.//*[#id='LayoutWrapper']/div[6]/div[3]/main/section[3]/div/div/div[3]/xpl-result/div/div[1]/div[1]/div[1]/span
.//*[#id='LayoutWrapper']/div[6]/div[3]/main/section[3]/div/div/div[4]/xpl-result/div/div[1]/div[1]/div[1]/span
is it possible to use a xpath command to take the total number of list which is in the previous case in 4?

The number of items selected by an XPath expression, xp, is given by count(xp).

Related

Find last element in dynamically changing xpath

//div[text()='abc']
It gives sometime 10 count, next time 20 and so on.
How can I find the last element in count even if the count changes dynamically?
Welcome to SO. You have to use last() xpath function to return a number equal to context size of the expression evaluation context, meaning it will get the size of the matching elements and you can access the last item by using the below standard xpath notation.
Rather specifying [1] or [5] you are passing the last item number.
(//yourxpath)[last()]
In your case the xpath will be
(//div[text()='abc'])[last()]
Here is the link with more explation
https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256083(v%3Dvs.100)

finding the current position of xpath match

I'm trying to get the current position of a xpath match. Here is a real world example
on this page http://newyork.backpage.com/homes-for-sale/
running the following xpath matches the 8th listing counting from top
//div[contains(#class, 'cat 93893742')]
I want to somehow get the ad position using xpath which at the time of posting this question is "8". I tried using prececeding-sibling::div but I am getting unexpected results.
Anyway to achieve this with xpath?
I'm not sure wether current version of htmlunit supports XPath 2.0, but if so you can use below expression:
index-of(//div[starts-with(#class, "cat")], //div[#class='cat 93893742'])
This will return 10 - position in common list
If you want to get position in list for specific date (Thu. May. 11) you can try:
index-of(//div[normalize-space()="Thu. May. 11"]/following::div[starts-with(#class, "cat")],//div[normalize-space()="Thu. May. 11"]/following::div[#class='cat 93893742'])
which returns 8
Some description added in #har07 answer
I think this is what you required
count(//div[contains(#class, 'cat 93893742')]/preceding-sibling::div[starts-with(#class,'cat')])+1
Lets breakdown the whole
//div[contains(#class, 'cat 93893742')]
will match the required context node which have classname = cat 93893742
/preceding-sibling::div[starts-with(#class,'cat')]
Will match the all div element starts with classname=cat just before your context node
So if we keep all those in count() it will count all div tag just before the context node So add 1 to include count of context node as well
If you want to point that element by using index like calculated above then add this
//div[starts-with(#class,'cat')][count(//div[contains(#class, 'cat 93893742')]/preceding-sibling::div[starts-with(#class,'cat')])+1]
Equal to
//div[starts-with(#class,'cat')][10] // 10 in index number
Based on this and your previous question, maybe the following XPath is what you're looking for :
count(
//div[contains(#class, 'cat 93893742')]/preceding-sibling::div[contains(#class, 'cat ')]
)+1

How can an Xpath be written for search results that change

I have a scenario where i am filling a search field with text 'A' and it returns a bunch of results. These results are always changing and i simply want to select the first 5 options. How is it possible to write an Xpath for this. I am trying to write an acceptance test using codecption
.//*[#id='js_search_table_filter'] - is the search table filter. I enter A with this below
$this->fillField($_sSelectSearchXPath,"A");
The xpath element when i click on the tick box option for the search results are like
.//*[#id='assign-9488'].
<label class="checkbox checked" for="assign-9488">
Note that the numbers vary and they are not in any chronological order.
I'm not sure if this is what you're after, but if you need the first 5 elements of a nodeset, you can suffix it with:
[position() <= 5]
Example:
<a>
<b>1</b>
<c>2</c>
<d>3</d>
<e>4</e>
<f>5</f>
<g>6</g>
</a>
If you want to get elements from b to f, you can run /a/*[position() <= 5].
I am not sure how codeception/php version works but in ruby (watir or pageobjectgem) you can identify all elements with a reqular expression or a partial match
the element would look like:
checkboxes(:your_element_name, :id => /assign/)
Notice the difference between checkbox and checkboxes which returns all checkboxes which have id containing assign.
In Java Webdriver, you have
driver.findElements(By.xpath("//checkbox[contains(#id,'assign')]"));
Both representatinos (ruby and java) return an array of elements where the Java solution is using xpath. The element you mentioned is a label, if you are looking for the checkbox, it is possibly a child element of this label and the xpath will be
//label[contains(#for,'assign')]/checkbox
Basically, you can use "contains" method in xpath to identify your element. You can then take the first five elements of the array.
I used this guys and tried to select all the tickboxes in the dropdown
(.//*[#type='checkbox'])[1]

XSLT/ Xpath how to accumulate / get total of a node based on a condition in another node

I need to get two totals CreditCardTotal and CashTotal and have to display them in another tag AccountCost, as shown below.
Basically, I need get the expense amount and check to see if it is a credit card or cash and then add it to the respective total variable. Or if there is a more elegant way please let me know.
I am completely stumped and new to Xpath. Thanks, and will truly appreciate your time and effort.
<ExpenseCatDetail>
<Expense>500</Expense>
<PaymentMethod>CreditCard</PaymentMethod>
<AccountCost>700</AccountCost>
</ExpenseCatDetail>
<ExpenseCatDetail>
<Expense>100</Expense>
<PaymentMethod>Cash</PaymentMethod>
<AccountCost>400</AccountCost>
</ExpenseCatDetail>
<ExpenseCatDetail>
<Expense>200</Expense>
<PaymentMethod>CreditCard</PaymentMethod>
<AccountCost>700</AccountCost>
</ExpenseCatDetail>
<ExpenseCatDetail>
<Expense>300</Expense>
<PaymentMethod>Cash</PaymentMethod>
<AccountCost>400</AccountCost>
</ExpenseCatDetail>
Element construction is not possible with XPath, you would require XQuery for that.
To fetch a single sum, use
sum(//ExpenseCatDetail[PaymentMethod="Cash"]/AccountCost)
and replace "Cash" as needed.
Using XPath 2.0, you could at least calculate both sums in one statement and return a sequence of both values (is usually mapped to an array or similar construct in other programming languages):
(
sum(//ExpenseCatDetail[PaymentMethod="Cash"]/AccountCost),
sum(//ExpenseCatDetail[PaymentMethod="CreditCard"]/AccountCost)
)

Use XPath to select the element with a certain token in the value

I have the following XML:
<ZMARA SEGMENT="1">
<MATERIAL>000000000030001004</MATERIAL>
<PRODUCT_GROUP>14000IAA</PRODUCT_GROUP>
<PRODUCT_GROUP_DESC>HER 30 AR NEW Size</PRODUCT_GROUP_DESC>
<CLASS_CODE>I046</CLASS_CODE>
<CLASS_CODE_DESC>Heritage 30</CLASS_CODE_DESC>
<CHARACTERISTICS_01>,001,PLANNING_ALERT_PERCENTAGE, 50.000,PLANNI</CHARACTERISTICS_01>
<CHARACTERISTICS_02>X,001,COLOR_ATTRIBUTE,Weathered Wood,WEWD,Col</CHARACTERISTICS_02>
<CHARACTERISTICS_03>,001,ARMA_UOM,SALES SQUARE,SSQ,ARMA UNIT OF M</CHARACTERISTICS_03>
<CHARACTERISTICS_04>,001,ARMA_A_CATEGORY,05-Below 260 Lam/Multi-l</CHARACTERISTICS_04>
</ZMARA>
Using XPath I need to select the CHARACTERISTICS_XX element whose value contains the COLOR_ATTRIBUTE token. It will not always be characteristics_02. Thanks for the help. I am a total noob at XPath.
This looks like its taken from a sap idoc, you can probably be lucky that the fieldnamed are not 6 character long abbreviations :)
The answer given by spinon is correct, however if there could be another element that contains the text 'COLOR_ATTRIBUTE', this would give a more specific match:
/ZMARA/*[starts-with(local-name(.), 'CHARACTERISTICS_')][contains(.,'COLOR_ATTRIBUTE')]
Another suggestion is to avoid the '//' expression if you know where the ZMARA element can occur, in the expression above ZMARA would only be searched as a root element which would be more performant.
This should work:
//ZMARA/*[contains(.,'COLOR_ATTRIBUTE')]

Resources