About

The following is a collection of notes on how to automate any website with hopefully zero knowledge about programming but with some knowledge on web technologies such as HTML and CSS (XPath, CSS selectors, and so forth.). The goal is to generate a workflow visually, such as logging in to a website, or clicking some buttons on a website, and then automate the recorded process on a server.

Installing Requirements

  • Selenium IDE browser addon, for Firefox the Selenium IDE can be downloaded on the Mozilla addons page and should be installed on the machine that will generate the automation workflow,
  • selenium-side-runner and a webdriver of choice must be installed, for this example, the Google Chrome webdriver will be used.

Assuming that nodejs is installed on the machine that will playback the workflow, the following commands install the selenium side runner:

npm install -g selenium-side-runner
npm install -g --unsafe-perm=true chromedriver

The –unsafe-perm=true parameter is required in case an error is received when trying to install the Chrome driver.

  • a browser, since this example uses the Google Chrome webdriver, the Google Chrome web-browser will be installed. Google Chrome can be obtained from Google. On Debian or Ubuntu, when attempting to install the package, an error about missing dependencies might be issued. Install the required dependencies, including X (unfortunately, even though it will not be used):
apt-get install xdg-utils

Now the following requirements should have been installed:

  • Selenium IDE browser addon on your work machine (client),
  • selenium-side-runner, chromedriver and Google Chrome on the machine that will execute the workflow (server)

Creating a Script / Workflow using Selenium IDE

This step is self-explanatory for the most part. Clicking the Selenium IDE addon button will launch a new window for the Selenium IDE. The principle is easy to grasp: you specify a website URL to automate, press the record button, perform a number of actions on that website (clicking buttons, filing in forms, etc.) and then play back the script to check that the script is performing all the actions properly.

In case the script fails to start at the very beginning, simply insert a new command at the very beginning of the script. Similarly, in case the Selenium IDE indicates an error mentioning CSP, on Firefox this error can be bypassed by going to about:config and setting security.csp.enable to false.

Here is a screenshot of an example script used to log-in to www.myanonamouse.net:

As can be observed, all the commands are listed as the script has been recording and if the play button is pressed, the script will perform all commands one after the other.

Once a script has been generated and tested to be working properly, the script can be saved by clicking on the diskette button to a file ending in .side (for example www.myanonamouse.net-login.side) that has then to be transferred to the machine that will perform the automation.

Running the Script Headlessly

With the side script transferred to the machine that will run the automation, in this example, www.myanonamouse.net-login.side, the script can then be executed headlessly using the Google Chrome web driver. For example, issue:

selenium-side-runner -c "goog:chromeOptions.args=[--headless,--nogpu,--no-sandbox] browserName=chrome"  www.myanonamouse.net-login.side

where:

  • www.myanonamouse.net-login.side is the script transferred from the previous step

Note that due to security restrictions, the Google Chrome webdriver will only run as a regular user on Linux. Running the script as the root user will generate errors that will lead to the Chrome webdriver to fail.

The script should now run and perform all the steps in the background without opening any visible browser windows. After running, selenium-side-runner will display some statistics such as whether the script ran successfully and the time it took for the script to run.

Running the Selenium Automation via Cron

Assuming that a Selenium IDE side-script named www.myanonamouse.net-login.side exists at /opt/myanonamouse/www.myanonamouse.net-login.side, the following script can be placed under /etc/cron.weekly in order to run the Selenium IDE script once a week:

#!/bin/bash
 
# Restart the script as an user via su.
if [ "$(id -u)" -eq 0 ]; then
    # exec replaces current shell
    exec su myanonamouse "$0" -- "$@"
fi
 
# Run the Selenium side script.
cd /opt/myanonamouse
selenium-side-runner \
    -c "goog:chromeOptions.args=[--headless,--nogpu,--no-sandbox] browserName=chrome" \
    /opt/myanonamouse/www.myanonamouse.net-login.side 2>&1 2>/dev/null >/dev/null

web/quickly_automate_websites_with_selenium.txt · Last modified: 2023/10/21 00:39 by office

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.