Passing browser instances in Power Automate Desktop

February 21, 2019

Introduction

In this article we will present how to work with the same browser instance through workflows in Power Automate Desktop and how to focus specific tab when there are multiple tabs.

Situation

nput and output workflow variables are the essential building blocks for making a decoupled RPA Process Architecture. Currently, Microsoft Power Automate Desktop does not support passing a browser instance as input or output variable.

A problem occurred when trying to use the same browser instance within more desktop flows. When you open another desktop flow, you must have browser instance to work with UI elements.

If you don’t want to produce a new one, you can’t continue working.

And if you want to produce a new browser instance, attached to the one already running, a problem occurs with working with multiple tabs – attaching to a running browser instance with multiple tabs open may not switch focus to the needed tab.

Why this is a problem?

Let’s assume you have automated 10, 50 or even 100+ processes in your company. Assume your ERP client is a web application, as many of current ERP nowadays (e.g., T24, JD Edwards, Dynamics 365…).

Many of these processes will require logging in into this ERP to read data or process transactional information.

Without the possibility to pass browser instances between workflows, you are forced to create a login section in each individual process workflow. Imagine now that with your 100+ automated processes, ERP login process is changed. You will need to make changes in each of the 100+ workflows that are using the ERP.

What to do

A simple solution to this problem has three steps:

First step – deconstruct Browser instance from the parent workflow as custom object. Be sure to include the current URL, page title and other variables that can help you to reference this browser.

Second step – Pass this custom object to the child workflow.

Third Step – In a child flow, attach to a running browser instance using the Title, URL or other parameters. If that does not work, use the Handle property from deconstructed browser instance.

We will create a desktop flow“Passing BI” in which we will initialize a browser instance and all required attributes, and after that we will create a second desktop flow “Receiving BI” that receives these information.

First step: Parent flow

Desktop flow – “Passing BI” setup:

First, we want to launch a new Browser and with it we are producing a new Browser Instance which we’ll use.

Variables that are required are URL’S to pages you want to work with. So:

  • Open new tab

for example, purposes and using variable URL. Variable produced is set to same “Browser” instance, and by that it is being overwritten.

Right after tab is opened, we are using

  • Get details of web page” action getting the “Web page title”,
  • “Get details of web page” action getting the “Web browser’s current URL address

just in case to be sure initial URL is not changed after opening page (in case it changes, we’ll overwrite our initial URL variable with correct value).

When we add those acitivites, workflow looks like this.

Web browser instance is not acceptable as input argument, so we cannot just pass it as it is.
On that account, we will create logic for passing necessary attributes by

  • initializing new variable as JSON

Next, we need to

  • convert it to custom object

and after that variable is ready to be sent.

Second step: Passing custom object to child workflow

At the end “Passing BI” desktop flow should look like this:

Before calling “Receiving BI” desktop flow and passing our custom object as input value, we need to set it up:

Third step: Child flow

Desktop flow – “Receiving BI” setup:

The idea is that “Receiving BI” called from “Passing BI” is not opening / launching new browser, but only focusing already opened browser tabs.

Although we can pass object with browser attributer, passing it doesn’t allow us to assign type “Web browser instance” but staying as custom object, therefore is not usable for working with UI elements which requires type “Web browser instance”.

Let’s set input variable “inCustomObject” as type ‘custom object’

so, when “Receiving BI” is called, we will have defined input variable, and we can pass JSON as Custom Object Value.

Next,

  • “Launch new browser” with “Launch mode” set to “Attach to running instance” and attach by title using passed tab title value (both title and URL are passed, so it’s your choice).

Unfortunately, this option alone will not change tab focus, although it will attach our new browser instance to the existing one.

For this reason, we are using

  • “Send keys” with keys shortcut for switching tabs by specified order

“CTRL + 2” for second tab in line with passed tab index value sent to window instance/handle “Browser” instance as in the image below

The last step is now at your disposal to choose actions for working with UI elements (e.g., get details of the UI element). Now we can work without any further delays!

Whole “Receiving BI” flow process is shown below:

For those situations where attaching to a running instance by URL or Tab Name is not possible, there is a possibility to focus on a desired tab by sending hotkeys using the process handle from the Browser object. Afterwards, we will attach to the current active browser instance, pre-focused using the process handle.

That way, we have the possibility to share browser instances between different workflows.

How this solution works?

This solution gives us the opportunity to not repeat our actions (e.g., opening and closing tabs) and reuse the same browser instance within different desktop flows. Makes the process more compact and easier to work with. You still manage the browser instance from the flow who call it but all code for login routine is in its own flow, so in the future you don’t need to waste your time editing your flow one by one if there is e.g., CSS change on the website. You just need to edit/update your login flow routine 1 time.