Selenium Speed Test
This blog is for learning purposes only, you can find the complete code on my Github go there to find the instructions necessary to run the code.
Background
The first time I ever used Selenium, I had to type a string into a search
bar. Doing so, I couldn't see if a string was typed at all, to the point
where I thought maybe it doesn't actually type words. I thought something
else was going on.
I am also kind of addicted to typing speed websites, I spend much more
time than I should on different sites where you're supposed to type some
words and see how many words you can type within a certain amount of time
(10fastfingers is such a website
https://10fastfingers.com). The
unit used to measure typing speed is words per minute, abbreviated as
WPM.
Looking at the high scores, it seems that the top typing speed is a bit
above 200 WPM. For someone like me who can score a 60 WPM on a good day,
this is extraordinary, but it seems that actual human beings are capable
of reaching such speed.
This is when I had the idea to do this small project, I want to see how
fast can Selenium score on 10fastfingers.
Code
In this section, I will briefly go over the code to explain how the
project works.
Let's start by launching Selenium, the latest version requires the use
of a "Service".
serv = Service(PATH)
driver = webdriver.Chrome(service=serv)
driver.get("https://10fastfingers.com/typing-test/english")
This will launch a browser to the simple English test
The first thing we want to do is to inspect the box containing the words, we need to grab all the words to be able to make Selenium enter each one of them in the input box.
As you can see, this is a "div" having an id of "row1", we can grab this div with Selenium like so
wordsContainer = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "row1"))
)
The code above waits up to ten seconds until an HTML element having an
id of "row1" appears on the screen, this element is saved in the
variable wordsContainer.
Once this is done, we want to collect the words inside this container,
which as we saw in the last image are located within "span" tags.
# Telling Selenium to wait for 10 seconds just to make sure the page will load completely
driver.implicitly_wait(10)
wordsContainer.find_elements(By.TAG_NAME, "span")
After giving some time for the page to load, we will find all the elements within wordsContainer that have a "span" tag, this is where the words are.
Parsing using requests-html
If you try to grab all the words using
wordsContainer.find_elements(By.TAG_NAME, "span"), you will only find around 20 words, the rest will be empty
strings!
I am not sure what is the reason for this, however, it doesn't matter,
we can use driver.page_source to view the
complete HTML of the page which will contain the "row1" div and all the
words inside of it.
Here comes the part where I use a scraping library called requests-html to parse the HTML from driver.page_source.
html = HTML(html=driver.page_source)
match = html.find("#row1",first=True)
words = match.text.split(" ")
The code above parses the HTML from the Selenium driver, then it locates
the "row1" div, and finally, it extracts the text inside of it which will
be a big string formed by each word within a span tag separated by a
space.(e.g. the string will be "next being another how could it's once ...")
The split method will be applied to this string to get a list of words.
['next', 'being', 'another', 'how', "could", "it's", "once", ...]
Typing With Selenium
Now, we need to type the words into the input field. Just like we did
previously, we need to select the input field where the words should be
typed, inspecting the input we have
this element has an id of "inputfield", so we need to locate it then type each word in the list words followed by the space key.
inputField = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "inputfield"))
)
for word in words:
inputField.send_keys(word)
inputField.send_keys(Keys.SPACE)
Here's a video of how fast Selenium is typing
As you can see Selenium completed all the words way before the countdown
finishes!
If you wait till the time ends (that's why I am using
time.sleep(60) at the end of the code),
10fastfingers will give you the following result
Pretty impressive! But this is not an accurate score at all, to find out
the actual score, we must get the exact duration when Selenium was
typing.
We can do so by getting the time displayed in the countdown and
subtracting it from 60.
The id of the timer is "timer", we will select it just like before, then we will extract the text from it, format it to get the final two characters which will hold the number we're looking for. Don't forget to cast it to an integer before doing any mathematical operations.
timer = driver.find_element(By.ID, "timer")
timer = timer.text[2:]
timer = int(timer)
print(f"{(len(words) * 60) / (60 - timer)} WPM")
Running the code again, we can find out the real speed of Selenium (at
least on my PC)
That's 960 WPM!