I have a requirment like, need to click one element in web portal, Portal has multiple behaviour some times it will load in same page, some time it will open new tab to load the data. I have close the additonal opened tab. Here i have static is title. I need to close the tab and as well before that i need to check is the tab is opened or not.
I have dealt with this exact scenario before, so let me share what worked for me.
Since the portal behaves differently each time - sometimes loading on the same page, sometimes opening a new tab - the trick is to check the tab count before and after the click.
what I did -
Before clicking the element, use Get Browser Tabs activity and store the result count in a variable, say intInitialTabCount.
Then do your click, add a small delay or Wait for Page Load to let the browser settle.
After that, Get Browser Tabs again and store it in intCurrentTabCount.
Now just put an If condition -
intCurrentTabCount > intInitialTabCount
If True - a new tab was opened. Since your title is static, use Attach Browser with the tab title to switch to it, do whatever you need, then use Close Tab and re-attach back to your original tab.
If False - no new tab, the page loaded in the same window. Just continue your flow normally.
What I would suggest from my experience :
After closing a tab, always explicitly re-attach to the main browser. Skipping this causes random failures down the line.
Don’t rely on fixed delays if you can avoid it. Use Check App State or Wait Element Appear instead - much more stable.
Wrap the close tab part in a Try-Catch just to be safe. Sometimes the tab closes before your activity fires and it throws an error unnecessarily.