How to break a While loop/Do while loop

How to break a While loop/Do while loop. Break activity is not working, seems it will work only for for each.



Maybe this will help,


1 Like


A While Loop/Do Loop, must have a condition to met. Try to do a If when you need to exit the loop which will give the condition to exit the loop :slight_smile:



I am clueless as to why the Try Catch method would not be considered as good practice.

Essentially you catch an exception (maybe element doesn’t exist, row end, pagination end etc etc) and update a value that does not satisfy the Do WHile/WHile VB Expression, eventually ending it. (An exception is basically developer defined and there is no hard and fast rule in that regard)

There have been requests for ‘Continue’ options and break options for While (not so relevant), but a workaround that helps you achieve the objective without shortcomings can also be considered.

Tell me what you think.

What about forcing to meet the condition.

For example if the condition is x < 10, then you can assign x = 10 when you want to end the loop

1 Like

I think that the TC (TryCatch) will work in some cases as a solution

The problem is that if an error is found in a step inside the loop you might want a different behavior rather that continue after the loop. For example, we have TC that restarts the process if an error is found to prevent to stop production.

1 Like

If you want to, you can use alternatively for loop. But for me, it would be more effective if you put do while inside a try catch and if you want to stop the respective loop, you can just throw an error.

1 Like


Do you have examples using While Break. I have a project that I am iterating though each row on my application page, but the number of lines changes for every invoice. I used a Do while with a condition of True, but of course it becomes an infinite loop. I tried the try catch option, but that did not work either.

Thank you

Hi @bakerjan,

Your particular scenario sounds like a For Each Loop would be a better choice.
If your invoices are in a spreadsheet, you could probably read the data into a DataTable and use the For Each Row activity to iterate through the data that way.


1 Like

Thanks for the quick response. Currently my query does not show how many lines. I can add the lines, but can’t figure out how to make it work.

Voucher/AP BU Invoice Number TAX CODE Lines
10101 9010058148 CA00 1
10101 9010057425 CA00 1
10101 9010057425 CA00 2
10101 9010057425 CA00 3
10101 9010057425 CA00 4
10101 9010057425 CA00 5
10101 971746196 CA00 1
10101 971746196 CA00 2


Where is your Voucher/AP BU table coming from?
How are you storing that data in memory?

I have a VB code that gets the URL from the Hyperlink in the spreadsheet. That takes me to a page in my applicate. Once the application is open to the correct invoice, I check to see if the invoice number matches, then preform a sequence of steps to fix the invoice error.

Create a while loop that will attempt to scrape all of the data on the screen with relative selectors (e.g. find the selector for drop down box 1 - in UiExplorer, make sure it has idx as part of the selector)
As you loop through the items on the screen, when you find the last item, stop the loop.
You can probably accomplish the 2 steps you have highlighted in the loop when you encounter them by using that same idx and the relative selectors for the voucher and taxcode inputs.

Thank you Kristopher, Sorry but I’m new to UIpath and this is my first project. My problem is I do not know what to put in the Condition when the number of iterations is unknow . Unless I’m missing something there is not a idx selector.