Browser Automation with Playwright (Robocorp)

Anyone who's automated a web browser using UiPath, Automation Anywhere, or Power Automate Desktop knows that these platforms offer browser extensions and low code activities. This greatly simplifies development for newcomers but has some limitations. In some cases, low code activities are not enough for certain complex browser interactions, requiring the use of JavaScript.

This is rarely an issue though. 95% of the time, low code activities are all you need. The bigger issue to consider is the problems caused by extensions:

  • Extensions must be installed on every machine the automation needs to run on (unattended or attended)
  • If your RPA platform releases updated versions of their extensions, you'll have to reinstall the updated extensions all over again
  • Older bots using older extensions may throw errors if extensions are updated
  • Access to Incognito/Private and file URLs are not enabled by default and must be enabled manually
  • Some organizations have security policies that prevent installing extensions

But it's possible to automate browsers without extensions using other libraries:

These libraries provide several benefits:

  • None of the extension issues mentioned above
  • Attended bots can use headless mode to prevent browsers from appearing and interfering with the user
  • Headless mode also allows bots to run in serverless environments (e.g. Docker)

Robocorp takes full advantage of all these benefits, making it a great starting point to learn about these libraries.

Robocorp offers keywords for Playwright and Selenium. Selenium keywords work out of the box, but before you use Playwright keywords, make sure to go through Robocorp's Playwright installation instructions. These keywords are wrappers for Playwright and Selenium's classes and methods, with the goal of improving and simplifying these two libraries for bot developers to use.

These keywords are good to use when developing bots using Robot Framework, but for pure Python bots, I recommend using the Playwright library itself rather than Robocorp's keywords. To do this, add the following to your conda.yaml file:

dependencies:
    - pip:
        - playwright==1.29.0

rccPostInstall:
    - playwright install

Note that installing the Playwright library itself is much more straightforward than Robocorp's Playwright installation (nodejs and robotframework-browser).

Now that you have the full Playwright library, you have access to all of Playwright's functionality, not just Robocorp's keywords. Extensive documentation is available at Playwright's site. For example, this snippet opens a browser, navigates to a site, prints the site's title, and closes the browser:

from playwright.sync_api import sync_playwright
browser = sync_playwright().start().chromium.launch()
page = browser.new_page()
page.goto("http://playwright.dev")
print(page.title())
browser.close()
Adapted from https://playwright.dev/python/docs/library#usage

Playwright has so many useful methods that discussing them all here would make this article much longer than it should be, but here are a few I've found useful:

Experiment a bit with Robocorp's Selenium keywords, Robocorp's Playwright keywords, and the Playwright library itself to see which one you prefer. Note the differences between each option (specifically exclusive keywords/methods not found in other options, and missing keywords/methods that other options have).

For example, (as of the time this was published) saving a webpage as a PDF is not possible using Playwright keywords. But crawling a site is only possible using Playwright keywords. There's a lot to consider, but whichever option you choose, you'll see that Playwright is incredibly powerful, especially compared to traditional RPA browser automation.

One last thing - for those of you with UiPath experience, if you try Playwright and end up loving it, Playwright has .NET support, meaning it can be used in UiPath. But unlike Playwright in Python, which can run asynchronously or synchronously, the .NET version only runs asynchronously. This means that extra code is needed to get .NET's async methods to run synchronously, since UiPath is synchronous and cannot await methods. This is certainly possible, but the code is very involved, and many will simply prefer using UiPath's browser activities.