How to know in each moment which page is currently present?

I’m trying to make login in a tricky site. After click in “Login” button could happen 4 things.

1-) Enters directly in main page. I this case I want to continue processing.
2-) After enter login, could appear a page requesting a code.
→ In this case I want that jumps to label “ENTER_CODE-PAGE”.
→ After enter the code could
2.1- access directly in main page (case 1)
2.2- Request solve a captcha. (In this case I want that waits until a human solve it and press “RESUME”)
→ After solve the captcha could enter directly to mainpage (case 1) or show an error (case 4).
4-) Could appear an error. In this case I want to finish the macro execution.
The error could happens in any moment after click in “login” button or after click in “verify code” button or after
click in “submit” when finish the captcha.

So, when happens case 1 is the easiest one and only need to continue with the main code.
When happens case 2, could go to case1 or captcha or error
and when requests captcha, could go to case1 or error.

I’m currently using “storeXpathCount” to check if some element that belongs to each case is present. After that I make if/elseif block.

The issue is that in some cases storeXpathCount doesn´t work (It seems due to iframe) and the other issue is how to evaluate which case is happening in every moment, since not always the cases appear in the same order or need to validate more than once.

May someone could give me some help in how to improve my current idea.

Below is the pseudocode I have.

Command         |           Target                                | Value
storeXpathCount | xpath = path_of_element_when_main-page_appears  | mainpage
storeXpathCount | xpath = path_of_element_when_code-page_appears  | code
storeXpathCount | xpath = path_of_element_when_error-page_appears | error
storeXpathCount | xpath = path_of_element_when_captcha_appears    | captcha

if	${mainpage} > 0
elseif	${code} > 0
elseif	${captcha} > 0
	pause | 0 | "Wait until a human solve the captcha and click Resume"
elseif	${error} > 0
	gotoLabel	END-MACRO