In a FOR loop, skip if text matches a defined exclusion?

Hello everyone - I’m looking for some help in skipping steps in a FOR loop if a condition is met.

Scenario: HTML table with one column and a variable amount of rows. I want to extract the text from all rows except the first.

Code:
${e6} = Get WebElements xpath:/html/body/table[4]/tbody/tr[3]/td[5]/table/tbody/tr/td/div
FOR ${item} IN @{e6}
log to console Charge(s): ${item.text}
END

…which produces this output:

Product(s): Mary's Cookie Shop  <------ *** I want to exclude this line ***
Product(s): Sugar Cookie
Product(s): Peanut Butter Cookie
Product(s): Chocolate Chip Cookie

Could anyone please suggest how I could format an IF statement within the FOR loop that would skip printing the ${item.text} variable to the console if the text matched “Mary’s Cookie Shop” else continue to follow the defined instruction?

Thanks!

Sorry folks I cannot seem to get the WSYWIG of this site to work properly to get the code into a snippet box and I also cannot edit the original post.

Hi James,

There are many ways to approach it, the best and simplest is to construct your xpath to only select elements you want, e.g. if the tr elements had an attribute of row like this:

<tr row=1>...</tr>

then changing your xpath to something like xpath:/html/body/table[4]/tbody/tr[3]/td[5]/table/tbody/tr[@row>1]/td/div might be all you need, without seeing the html I can’t tell you exactly what that would look like

Another simple approach is to just use a counter and tell the if statement to only execute if the counter is higher than a particular number, of course this only works if you only want to exclude the first n items, that would look something like this:

${e6}=    Get WebElements  
 xpath:/html/body/table[4]/tbody/tr[3]/td[5]/table/tbody/tr/td/div
${countter}=    Set Variable    0
FOR    ${item}    IN    @{e6}
    IF    ${countter}>0
        log to console     Charge(s): ${item.text}
    END
    ${countter}=    Evaluate    ${countter}+1
END

Hope that helps.

Dave

1 Like

Hi Dave,

Thank you much! I was actually able to solve my original problem, but today I am facing a situation where I need a counter. I was actively looking into that when I got the notification of your reply here. What a small world!

Regarding the original problem, I found the exact solution I was looking for and it was very simple.

${itemx}  Mary's Cookie Shop

${e6} =  Get WebElements  xpath:/html/body/table[4]/tbody/tr[3]/td[5]/table/tbody/tr/td/div
FOR  ${item}  IN  @{e6}
  IF  '${item}'=='${itemx}'  CONTINUE
  log to console  Product(s): ${item.text}
END

If the extracted value matches my defined value the continue statement will skip the steps as desired and move to the next sequence.

I was able to find this information here.

1 Like

Hi James,

Yeah thats another way to solve that problem.

There is an old joke: give 100 programmers the same problem and you’ll get different 100 right answers :rofl:

Glad I could help.

Dave.

1 Like