Krushi Raj Tula / Writings / Quiz writer bot project /

Image from Google Images

Quiz Writer Bot

January 22, 2019

1 mins.

Technology tags:

I have made this project for my personal use and it helped me in a few situations after I wrote this one. You can read about the background story about why I built this. You can also find the basic version of the script for the bot at:

In this post, I’ll briefly explain how I made this bot and what were the problems that I faced and the decisions I made.

Why this bot

First and foremost thing that motivated me to write this bot is that almost 80-90% of questions were based on guess the output types. So, I thought I can write a bot which can directly execute the code that is shown and select the answer based on output. If you want to read in detail about why I built this then read Quiz writer bot blog

Identify the pattern

To the knowledge that I had at the time I wrote this bot, I know that bots are written based on some common patterns for a given task. The common things or patterns that I found in my particular problem were:

  1. All questions containing code to guess the output were in C (a single language)
  2. Most of the options contained the exact output that we may get when we compile and run the given code. No complicated options that required human intervention to map to an option based on the output.
  3. The DOM pattern was similar to all questions, they don’t have any dynamic class names or ids. So once we have an XPath or a CSS selector to some element it remains the same all the time.

Once I had all the information above I tried to fetch the elements by playing for a while in console in developer tools of the browser. With that, I have all the CSS selectors or XPaths ready for grabbing the elements.


Now I can access the DOM elements that I want but, what about the interactions? How do my bot clicks on correct options and then clicks on the next question, next test, etc.,? To simulate these I chose Selenium, which is used to automate some tests on webpages. My choice of language was python because of 2 reasons, I am very elegant in Python and most of the bots that I heard of is written in Python. So, here is what I did:

  1. Initialize the selenium driver and goto the URL where the online test is hosted. Enter my credentials which are fed to bot through selenium.
  2. Select the appropriate test and click on start test.
  3. Repeat the following steps until there are no questions left else go to step 9.
  4. Get the question and find if it contains ‘guess the output’ sub-string.
  5. If no skip to next question else get the code from pre or code tag and write it to a file with .c extension.
  6. Now compile the file and execute it with the run method exposed in the subprocess module of python and get the output.
  7. Compare the output the options available. Whichever has the exact match select it. If no exact match is found then go for the next closest match. If still nothing matches then go for some random option.
  8. Click on the next question.
  9. Click on the next test if there are any else quit the browser. I used difflib for finding the closest matches and random module for random choice making.

That’s how I wrote a bot to write online tests for me in my hectic schedule 😜.

Krushi Raj Tula

By Krushi Raj Tula A developer, geek, enthusiast, who loves to solve problems and fix things with technology. You can reach out to him on Twitter!