How to use an already running (Chrome) browser for your web automation robots
Learn how to take over control of your already running browser with your rpaframework automation scripts using the `Attach Chrome Browser` keyword.
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.
Let's see how!
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.
For these changes to be applied, you must close all currently open instances of Chrome.
On Windows, you can run this command in the command prompt:
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:
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:
- On macOS:
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.
Here's how this robot looks like when run: