I need to click on a “account” button from a table of buttons. If this “account” button is not present, then I will need to click a different “UseAnotherAccount” button.
How I do it at the moment is:
- Set a flag
account_found
to be false by default - use a foreach loop to search through the whole
_list
of buttons - If the expected account button is found, click it, set
account_found
flag to be true and break away from thisforeach
loop - an additional if block is used to to click on
UseAnotherAccount
button ifaccount_found
flag is not set.
My code is shown below:
bool account_found = false; foreach (DivTag element in _list) { String account_name = element.GetInnerHtml(); if (account_name.Equals(_account_name)) { element.Click(); account_found = true; break; } } if (!account_found) { _my_repo.SignInToYourAccount.UseAnotherAccount.Click(); }
I have this feeling my way is not the most elegant way to achieve this. Any suggestions?