Browser task automation with Firefox and CSV files

This article is about automatically filling out web forms with data from a CSV file using Selenium and Firefox Browser. The example script provided below is a boilerplate for bulk creation of email accounts in Plesk Onyx and can be directly used for this task. To perform any other tasks, you have to change the script to your needs.

UPDATE (2018–03–19): Since Mozilla changed their addons-policy, Selenium is only available for Firefox versions 53 and below. Download the latest compatible version here: https://ftp.mozilla.org/pub/firefox/releases/53.0/

Sometimes you just want to go through a CSV file, fill out a web form with the data, click a button and proceed to the next line. Sounds like a perfect task for an automated script. There are some commercial tools for task automation and some very complex workflows to import data from Google Sheets, etc. Especially when you are working with passwords to create user accounts, you may not want some external service to process it.

I want to show you a solution with Selenium IDE, an addon for the Firefox Browser. Selenium is normally used for automated website testing. You tell Selenium what to do on a website, which buttons to click and what to write into the forms. Selenium can then repeat the same steps over and over again and would notify you if something does not work as expected.

For reading from a data source, there is a CSV plugin for Selenium IDE itself. All you need is the CSV file with your data and some scripting (thanks to ElectricRefugee at Stackoverflow) and you are able to create for example hundreds of user accounts in some minutes.

Setup Steps

  1. Install Firefox Browser
UPDATE (2018–03–19): Since Mozilla changed their addons-policy, Selenium is only available for Firefox versions 53 and below. Download the latest compatible version here: https://ftp.mozilla.org/pub/firefox/releases/53.0/
  1. Install Selenium IDE addon for Firefox browser
  2. Install Selenium Selblocks addon for Firefox browser. This will allow easier cycling through the CSV file
  3. Download the CSV plugin and store it in a permanent place on your disk
  4. In Firefox go to “Extras” > “Addons” and open the preferences of Selenium IDE. In the “Selenium Core extensions” settings click “Browse” and select the CSV Plugin file. Click OK and restart Firefox
  5. In Firefox go to “Extras” > “Selenium IDE” which will bring up the interface. In the “Table” tab you will enter the steps that the browser should perform. In the “Source” tab you will find a code representation of the steps in XML format. Here you can Import and Export whole automation scripts by copying and pasting the XML
  6. To get you quickly up and running I created a full example which you can find at the end of this article. Just copy the code and replace all the code in the “Source” tab with it. Then go back to the “Table” tab. You should now see a longer list of commands. Change the values by clicking on an entry in the table and changing the fields in the form below. You can add/delete commands by right-clicking in the table. The purple lines are just comments I added in between for explanation. They can be removed. Note that the CSV file should be plain text with commas as column separator and double quotes for the values
  7. Hit the “Play entire test suite” button to execute the script. Firefox will then automatically execute all the defined steps

Example Selenium script in XML format

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="" />
<title>Task Title</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">stauffenberg</td></tr>
</thead><tbody>
<!--Set target to the amount of lines in the CSV file-->
<tr>
<td>store</td>
<td>100</td>
<td>loop</td>
</tr>
<!--Initializing the counter with value 1-->
<tr>
<td>store</td>
<td>1</td>
<td>p</td>
</tr>
<!--Starting the loop. Everything in the loop will be performed for every line in the CSV.-->
<tr>
<td>while</td>
<td>${p}&lt;${loop}</td>
<td></td>
</tr>
<!--Open website-->
<tr>
<td>open</td>
<td>https://www.my-plesk.com:8443/smb/email-address/create/domainId/1</td>
<td></td>
</tr>
<!--Example: Uncheck the checkbox with ID-->
<tr>
<td>uncheck</td>
<td>id=general-generalSection-loginAsUser</td>
<td></td>
</tr>
<!--Open the CSV file, set the target to the CSV file-->
<tr>
<td>readCSV</td>
<td>file:\\/Users/Me/Desktop/mail.csv</td>
<td></td>
</tr>
<!--Save the first column of the current CSV line as "address" variable-->
<tr>
<td>storeCellValue</td>
<td>address</td>
<td>${p},1</td>
</tr>
<!--Save the second column of the current CSV line as "password" variable-->
<tr>
<td>storeCellValue</td>
<td>password</td>
<td>${p},2</td>
</tr>
<!--Insert "address" value into field with ID-->
<tr>
<td>type</td>
<td>id=general-generalSection-name</td>
<td>${address}</td>
</tr>
<!--Insert "password" value into field with ID-->
<tr>
<td>type</td>
<td>id=general-generalSection-password</td>
<td>${password}</td>
</tr>
<!--Insert "password" value into field with ID (In this case password confirmation field)-->
<tr>
<td>type</td>
<td>id=general-generalSection-passwordConfirmation</td>
<td>${password}</td>
</tr>
<!--Increase the counter value by 1-->
<tr>
<td>storeEval</td>
<td>${p}+1</td>
<td>p</td>
</tr>
<!--Click the button with name-->
<tr>
<td>clickAndWait</td>
<td>name=send</td>
<td></td>
</tr>
<!--End the loop and proceed with next line-->
<tr>
<td>endWhile</td>
<td></td>
<td></td>
</tr>
</tbody></table>
</body>
</html>