How to use an already running (Chrome) browser for your web automation robots

When writing robots that automate web applications with the rpaframework set of libraries, the most common first step is to open a "fresh" browser using the Open Available Browser or Open Browser keywords. By default, this new browser will not have your log-in information, or plugins that you might have installed.

There are, however, many uses cases in which it is useful to allow a robot to operate on an already running browser. For example, a browser where a human has logged into an account that requires two-factor authentication, or where some manual operations that might be hard to automate have already been done, and now the robot can take over to complete the process. Or you could automate some intermediate part of the process, then giving back control to the human to complete it.

If you use Google Chrome, to support these type of use cases you can use the Attach Chrome Browser keyword, provided by the RPA.Browser library, part of the rpaframework set of libraries.

Let's see how!

This feature was added in the 2.5.0 version of the rpaframework, so make sure you are using version 2.5.0 or newer.

1: Run Chrome with the remote debugging protocol enabled, and take note of the port

Your robot will communicate with Chrome via the remote DevTools Protocol, which is not usually enabled when you run Chrome just by clicking on the Chrome icon in normal desktop operations.

So, you will need to enable the protocol by starting Chrome with the --remote-debugging-port command-line switch, and choosing a port for it. Port 9222 is used by convention, but you can use any non-reserved port.

On Windows, you can run this command in the command prompt:

start chrome.exe -remote-debugging-port=9222

You can use the same technique for macOS or Linux. In this case, you will need to find the path to the Chrome executable. Here's an example with the default path for the Chrome executable in macOS:

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222

Non command-line based ways of running Chrome with debugging enabled

You might want to make life easier for your users, and allow them to launch Chrome with the remote DevTools protocol enabled without the need to use the command line:

  • On Windows, you could create a shortcut on your desktop, associating the command with it: Create shortcut for windows
  • On macOS:
    • You could create an Automator workflow that runs a bash script to open the browser with the command line switch enabled.
    • You could use the .dmg file that you can obtain from this GitHub repository.
    • You could create your executable with a tool like Platypus.

2: Use the Attach Chrome Browser keyword to take control of your browser!

Once you have Chrome running with the remote DevTools protocol running, you can execute any robot with it using the Attach Chrome Browser keyword.

In this example robot, we are opening the Google front page and searching for a keyword, using an already open instance of Chrome which is listening on port 9222. Before we run our robot, the browser could be pointing to any other webpage, being logged-in into any service, etc.

*** Settings ***
Documentation     Executes a search on Google.com using an already open Chrome browser
Library           RPA.Browser

*** Tasks ***
Attach to running Chrome Browser and execute Google search
    Attach Chrome Browser    9222
    Go To    https://www.google.com/?hl=en
    Input Text    name:q    Dinosaur
    Press Keys    name:q    ENTER

Here's how this robot looks like when run:

Attaching to running Chrome Browser