Persisting Browser Sessions in Robot Framework & RPA Framework

Using Robot Framework and accessing sites that require a login?  Sure, you can login every time with the same credentials but there may cases where that doesn't make sense.

This post will cover how to use the Chrome driver and pass a user-data-dir argument to keep your bot logged in between runs.

Prerequisites

You will need to have chromedriver on your path.  If you don't have it installed or aren't sure how to follow this note.  There is also a python package that will install and manager your drivers but you may still need to add the chromedriver to your path.

You will need to define an environment variable to USER_DATA_PATH which should be the path to your Chrome user data path.  Depending on your platform this will differ.  For me, this looks like %LOCALAPPDATA%\Google\Chrome\User Data (reference)

The Keyword

*** Settings ***
Documentation   Logs into gmail
Library         RPA.Browser

*** Keywords ***
Open Browser Profiled
   [Arguments]    ${url}
   ${user_data_path}=    Get Environment Variable     USER_DATA_PATH
   ${options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
   Call Method    ${options}    add_argument    --user-data-dir\=${user_data_path}
   Create WebDriver    Chrome    chrome_options=${options}
   Go To    ${url}

*** Tasks ***
Open Gmail
   Open Browser Profiled      https://www.gmail.com

Let's break this down line by line.

*** Keywords ***
[Arguments]    ${url}

👆 This line accepts the first arg of the page URL we are accessing

*** Keywords ***
...
${user_data_path}=    Get Environment Variable     USER_DATA_PATH

👆 This line is getting the environment variable to our user data directory on the machine.

*** Keywords ***
...
${options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver

👆 This line is getting the chrome options from the selenium.webdriver module.

*** Keywords ***
...
Call Method    ${options}    add_argument    --user-data-dir\=${user_data_path}

👆 This line is where the magic happens.  This is setting the data directory where your session data and cookies will be saved.  Call Method is just that, it calls a method on the item it is passed.  In this case the Chrome Webdriver's options.

*** Keywords ***
...
Create WebDriver    Chrome    chrome_options=${options}
   Go To    ${url}

👆 We pass our options to the Chrome Webdriver constructor.

If you already know how to do this with Python, this post really just goes into how to transpose Python to Robot Framework.  

Here is the same notion expressed in Python:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(f"--user-data-dir={USER_DATA_PATH}")
webdriver.Chrome(chrome_options=options)
webdriver.get(url)

If you are asking yourself, why on earth wouldn't you just write this in Python?  You might want to give this episode of the podcast a listen.  In short, it helps to self-document your automation, promotes code reuse, abstraction and can help with configuration.

Keep in the Loop

Get the latest news and exclusive releases

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.