Automate the typing test websites with Python

Published by on December 28 2020

This article was adapted from coderasha's article on Dev.to, found here: https://dev.to/coderasha/automate-the-typing-test-websites-with-python-abc

In this post I will show you how to automate the typing test websites which is really fun and by building this project you will improve your automation skills with Selenium.

Basically, these kind of websites are generating collection of words and allows user to test his typing speed by measuring the entered word count. I always wanted to took first place but usually my speed was not higher than typical programmer. So, I decided to automate this action by creating a typing bot with the help of Selenium and Python.

I am assuming that you have already created a new working directory so let's start by creating a virtual environment to isolate dependencies and then install Selenium in order to work with automation:

virtualenv env env\Scripts\activate pip install selenium
1
2
3

If you are using Linux as operating system then you can activate the virtual environment by following command:

virtualenv env source env/bin/activate
1
2

Once you installed the selenium, then we will need chromedriver to execute selenium scripts in Chrome as well as automate any website on the internet. First you need to know which version of chromedriver is compatible with your Chrome browser. If you don't know the current version of your browser then follow the steps below:

Open Google Chrome
1.
Click the three dots in the upper right corner of the window
2.
Hover your cursor over the "Help" option
3.
Click the "About Google Chrome"
4.

The current version of your Chrome browser will appear on the next window toward the top of the screen. At the time of writing this post the last version is 87 so I must download chromedriver 87 in this case. Go to the following link in order to download chromedriver and select the proper version from the list: https://chromedriver.chromium.org

Once you downloaded, extract the zip folder and copy chromedriver.exe and paste it to your current working directory. Great! Now, we have our driver so it's time to test if works properly.

automate.py
script

Continue scrolling

Start by importing the webdriver from selenium module

Then create a new variable named driver and assign it to executable path of Chrome. The executable path must be correctly pointed to the chromedriver and since we copied the driver in our working directory you can easily find path by checking properties of this file from file explorer.

As you noticed the path surrounded with r' ' quotes that means the string will be treated as a raw string and all escape codes will be ignored. Alternatively, you can use double forward slash but for now let's keep it in this way.

It's time to review our target website by navigating the following URL: https://typing-speed-test.aoeu.eu

Basically, you will see a container filled with collection of random words and user have to type each word then click to space button to get next words. Copy the URL above and pass into driver.get() function

Now, we can launch the script to see if it's actually working. Open command prompt or terminal, navigate your current directory and start the script. I named the file typing.py so in my case I will run the following command

If it didn't work, please check the executable path of your driver as well as the version compatibility. Next, open inspect element by pressing f12 button on your keyboard then we need to get the active word which platform expects to be entered inside input area. Now, if you hover the cursor on the active word you will see it has ID property named currentword

Try to type the words and you will see this ID will be assigned to next word each time which means there are few milliseconds of delay while assigning the ID. Selenium provides explicit waits which is waiting for certain condition to occur before proceeding further in the code.

We are going to use expected condition function which returns a boolean true in case of success or not null if it fails to locate the element and by using WebDriverWait selenium will wait maximum of 10 seconds for an element matching the given criteria to be found.

Once we get the current word it needs to be passed inside input and if you inspect that element on website you will see it has ID named input so let's define this element by using standard selenium locators

In order to start the timer, we have to click the input field because there is a click listener which triggers timer once the input state becomes focused.

Then, the program must click the space button to get the next word and we can achieve that by importing Keys module

At this point, we will use for loop with range function to iterate through the words. Open inspect element again to check other words and you will see they have same class name which is nextword so we can use it to find total number of words

By using find_elements_by_class_name locator we can get the list of these elements and since we only need the length, let's surround it with len function

After that, define a for loop with a range function to iterate through the list. As you noticed, we added plus one to the length in order to calculate first word as well because it will be not included to list due to the assigned ID.