Gracefully closing a specific tab among multiple tabs

My problem is this: I have multiple tabs open. At some point in the process automation I would like to “gracefully” logout and close a specific tab, among a mixed bag of tabs. I’m using Edge browser. By “graceful”, I mean, check if the specific tab is available. If the tab is available, switch to that tab, check if we are logged in, if so, logout.

It is possible that the tab is no longer available.
It is possible the tab is available but already logged out from the web service.

Now, I started with a test of three open tabs, all different addresses.
First tab: www.duckduckgo.com
Second tab: www.saucedemo.com
Third tab: www.google.com

Since they can be selectively distinguished from each other, I thought I could use ElementExists activity. During testing, this activity will return true, only if the tab is actually the current open active tab. If one of the other tabs is the active one, ElementExists will return false. This activity is dishonest (not true to it’s intended use) it only gives the correct boolean value if the tab is in the foreground. If one of the other tabs are active, it gives false, saying “This tab is not here.” But it is. It’s just not the currently selected tab.

I ended up with a try-catch block. Inside the try, I do AttachBrowser with the selector of the specific tab I wish to attach to, if it is still available. Remember, It could already be closed. The AttachBrowser has a timeout of 3s. Hence, It is inside a try block because it will generate an exception after 3s if it cannot attach to the tab (because it is no longer available). The catch block will catch a SelectorNotFound exception if the AttachBrowser have not found the tab in 3s.

This solves the problem of finding out if the tab is available. And also attaches to it if it is available. But does anybody else cringe when they use try-catch in programming? :slight_smile:

Anybody knows of a better way, than please tell me. Ideally, I think we would like to be able to rely on ElementExists activity, or something like it.

Next problem was that after using AttachBrowser to the specific tab, the tab is not brought to the foreground. Should It really be like that? Would It not feel more intuitive to actually see the tab you attach too? Is it just me? I ended up using a SendHotkey, Ctrl-l to activate the address bar of the tab I had just attached to. Now the tab is brought to the foreground. Before using the SendHotkey activity, I had tried using SetFocus activity. It did not work. Another dishonest activity? I mean… come one! *sight*

Little things like this makes me lose my marbles…

AttachToSpecificTab.xaml (8.2 KB)

To run, just open edge/chome browser with the links that were mentioned above, and then run the flow.