Selenium is a fantastic tool for automating your browser — and it can be used not only for writing functional tests, but also to automate mundane tasks such as downloading bank statements. However, very often, automating a web page involves a complex sequence of tasks (often including signing in) which makes traditional iterative development (code, test, repeat) cumbersome.
This article demonstrates how one can leverage the power of Python combined with Selenium to allow to quickly explore web automation by dynamically running code on-the-fly.
Prerequisites
- Python 3 (tested with 3.8 but should work with other versions as well)
- Selenium for Python (pip install selenium)
- Chrome driver (https://chromedriver.chromium.org/downloads) accessible via PATH
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
The following example demonstrates searching for “George Washington” on Wikipedia:
Tip: One can check vars() and globals() before defining a variable. For example, the following code saves the name of the current window handle:
if not ('main_window_handle' in vars()
or 'main_window_handle' in globals()):
print('main_window_handle is not defined')
Tip: Once you’ve ran the first file, you will have a new Chrome window running and connected to the driver. Use Developer Tools to explore the DOM and to drive the experimentations in the dynamic code.