Automate the typing test websites with Python
Tags: python, automation, beginners
Published: 2020-12-27 08:01:19

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
```
If you are using Linux as operating system then you can activate the virtual environment by following command:
```
virtualenv env
source env/bin/activate
```

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:

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

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. Start by importing the webdriver from selenium module then create a new variable named driver and assign it to executable path of Chrome.

```python
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'path\to\chromedriver.exe')
```

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 like below:

```python
driver.get("https://typing-speed-test.aoeu.eu")
```

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 by typing *python filename.py*. I named the file typing.py so in my case I will run the following command:

```
python typing.py
```

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 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.

```python
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

...

word = WebDriverWait(driver, 10).until(
   EC.presence_of_element_located((BY.ID, "currentword")))
```

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:

```python
input_area = driver.find_element_by_id("input")
input_area.click()
input_area.send_keys(word.text)
```

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:

```python
from selenium.webdriver.common.keys import Keys

input_area = driver.find_element_by_id("input")
input_area.click()
input_area.send_keys(word.text)
input_area.send_keys(Keys.SPACE)
```

Great! The main structure of the program is ready. Now, try to run the program and you will see the automation only applied for the first word, not all of them. So, this process needs to be executed continuously once all the words finished. 

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.

```python
total_words = len(driver.find_elements_by_class_name("nextword"))
```

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 and indent the rest of code to complete everything. The full code will look like below:

```python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 

driver = webdriver.Chrome(executable_path=r'path\to\chromedriver.exe')
driver.get("https://typing-speed-test.aoeu.eu")

total_words = len(driver.find_elements_by_class_name("nextword"))

for word in range(total_words + 1):
   word = WebDriverWait(driver, 10).until(
     EC.presence_of_element_located((BY.ID, "currentword")))

   input_area = driver.find_element_by_id("input")
   input_area.click()
   input_area.send_keys(word.text)
   input_area.send_keys(Keys.SPACE)
```

As you noticed, we added plus one to the length in order to calculate first word as well beacuse it will be not included to list due to the assigned ID. 

Great! Now run the program and enjoy watching how fast actually automation is typing. You definitely will take the first place!

#WATCH THE RESULT ON YOUTUBE
[![coderasha](http://img.youtube.com/vi/xJ8qM5aLNpI/0.jpg)](http://www.youtube.com/watch?v=xJ8qM5aLNpI)

Follow me on IG for more: [@coderasha](https://www.instagram.com/coderasha/)

Thumbnail References:
[Robot Designed by Freepik](https://www.freepik.com/vectors/technology')
[Typewriter - Designed by Zirconicusso](freepik.com)