Exploring Web Automation with Dynamic Code

Prerequisites

Approach

The main idea is to separate the automation into two pieces of code, residing in separate files:

  • The first file initializes the driver, does some initial setup (e.g. navigating to the page) and then starts the polling loop that automatically loads and executes dynamic code.
  • The second file contains code that gets executed automatically whenever the file changes (usually when the user saves it).

Main File

Dynamic File

The dynamic file can contain arbitrary code. Additionally, it can:

  • Access any variables defined in the main file (including “driver” which gives access to the Selenium driver)
  • Define new variables that will persist between invocations
if not ('main_window_handle' in vars() 
or 'main_window_handle' in globals()):
print('main_window_handle is not defined')

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store