Xpath and logic help

Hi Everyone,

I have a test case that needs to loop through certain products (10 products) and find the product that has the the most coveo-result-color-option, however, when I loop through the products, it give me all the coveo-results-color-options finds under it (which is 21 of them), instead of the stop at one product and count the coveo-results-color-options under that one product. I try to change the xpath but at this point I am not sure if this is not the right approach to solve this or if I am not giving the correct xpath.

This is html for the total products (10 of them) that I would like to loop through

Under each div, there is a class called result-color-options

My approach to solve this is to loop at the product level (10 products) and then under each product (one of those 10 products) if the resulr-color-options count is more than 3,
then I would like to exit the loop, but, with the xpath I have, it seems to looking for all the result-color-options for all 10 products which was always returning 21.
What can I improve here to make this work? Any help is really appreciated

This is the logic that I have for this test case

Is the condition to find the product with the most color options always that the div, with the class “result-color-options” should have > 3 coveo-results-color-options?

If so you can use a xpath like //div[@class=‘product-listing__result’][div[@class=‘result-color-options’][count(div[@class=‘coveo-result__color-option’])>3]]

Hi @Pari,

Perhaps a different approach

an xpath like this should give you all the <div class=“product-listing__result”> that have a <div class=“coveo-result__color-option”> inside

//div[@class="product-listing__result" and div/div[@class="coveo-result__color-option"]]

So we’ve eliminated the product-listing__result that don’t have coveo-result__color-option

So with that we can get a count with Get Element Count

Then use IN Range instead of IN when iterating over the product-listing__result so we can use the xpath to specifically address the product-listing__result we want to count the coveo-result__color-option of

e.g. to get the count of result__color-option for the 3rd product-listing__result we would use an xpath like this:
(//div[@class="product-listing__result" and div/div[@class="coveo-result__color-option"]])[3]//div[@class="coveo-result__color-option"]

Putting it all together it would look a bit like this:

${cPL}=    Get Element Count    //div[@class="product-listing__result" and div/div[@class="coveo-result__color-option"]]

FOR    ${i}    IN RANGE    ${cPL}
    ${cCO}=    Get Element Count    (//div[@class="product-listing__result" and div/div[@class="coveo-result__color-option"]])[${i}]//div[@class="coveo-result__color-option"]

To make it a bit more maintainable and easier to read you might want to use a variable for the base xpath:

${basexpath}=    Set Variable 	  //div[@class="product-listing__result" and div/div[@class="coveo-result__color-option"]]
${cPL}=    Get Element Count    ${basexpath}

FOR    ${i}    IN RANGE    ${cPL}
    ${cCO}=    Get Element Count    (${basexpath})[${i}]//div[@class="coveo-result__color-option"]

Hopefully that helps,