Automating Unitymedia Speedtest with Selenium

Posted on February 5, 2018. Tagged as: ,

My internet connection was slow intermittently, so I needed a way to automatically collect throughput information several times a day. The internet service provider (Unitymedia in this case) has a speedtest page that's pretty useful for that, but it requires manual clicking a button every time. Not very practical for automated collection.

Turns out with Selenium this is pretty easy to automate. I did use the python API with chrome on Ubuntu 17.10, with following packages:

The example code below runs the speedtest, collects the results and prints them to stdout in a somewhat structured way after the test run.

Update (2018-02-05): The script collects ping time as well now.

#!/usr/bin/env python3

import time
import sys

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait

browser = webdriver.Chrome(executable_path="/usr/lib/chromium-browser/chromedriver")
browser.get('https://speedtest.unitymedia.de/')
browser.implicitly_wait(2)

assert 'Unitymedia' in browser.title
browser.find_element_by_link_text("Weiter zum Internet Speedtest").click()
browser.find_element_by_id("icon-unitymedia-cable").click()
browser.find_element_by_class_name("speedtest-btn--primary").click()

result_link = (WebDriverWait(browser, 60)
               .until(lambda x: x.find_element_by_link_text("Details & alte Ergebnisse ansehen")))
result_link.click()

browser.find_element_by_class_name("speedtest-toggle-header").click()
results = browser.find_element_by_class_name("speedtest-toggle-header")
results_id = results.get_attribute("data-test-id")
results_day = results.find_element_by_class_name("day").text
results_time = results.find_element_by_class_name("time").text
results_download = (results
                    .find_element_by_class_name("download")
                    .find_element_by_class_name("number")
                    .text)
results_upload = (results
                  .find_element_by_class_name("upload")
                  .find_element_by_class_name("number")
                  .text)

result_details = browser.find_element_by_class_name("speedtest-toggle-wrapper")
result_ping = (result_details
               .find_element_by_class_name("ping-result")
               .find_element_by_class_name("speed")
               .find_element_by_class_name("number0")
               .text)

print("{};\t{};\t{};\t{};\t{};\t{};"
      .format(results_day, results_time, results_download,
              results_upload, result_ping, results_id))
browser.quit()