Created
September 1, 2018 21:56
-
-
Save J0s3f/80d9ecb25db7639b190e1e0ab76bd620 to your computer and use it in GitHub Desktop.
RTR Netztest python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium import webdriver | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.common.exceptions import TimeoutException | |
import selenium.webdriver.firefox.webdriver as fwb | |
from selenium.common.exceptions import TimeoutException | |
import os,datetime,csv | |
import pickle | |
#log_path = os.path.join(os.path.dirname(__file__), '{}.log'.format(datetime.datetime.now().isoformat('_'))) | |
#log_file = open(log_path, 'w') | |
#binary = FirefoxBinary('/usr/bin/firefox-esr', log_file=log_file) | |
binary = FirefoxBinary('/usr/bin/firefox-esr') | |
browser = webdriver.Firefox(firefox_binary=binary,timeout=60) | |
try: | |
#browser = webdriver.PhantomJS() | |
#browser.set_window_size(1120, 550) | |
try: | |
browser.get('https://www.netztest.at/') | |
cookies = pickle.load(open(os.path.join(os.path.dirname(__file__),'cookies.pkl'), "rb")) | |
for cookie in cookies: | |
if cookie['name'] == 'RMBTuuid': | |
cookie['expiry'] = int((datetime.datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)-datetime.datetime(1970,1,1)).total_seconds()+1209600) | |
browser.add_cookie(cookie) | |
print ("added cookie ", cookie) | |
except Exception: | |
print ("Couldn't load cookies") | |
browser.get('https://www.netztest.at/de/Test') | |
delay = 30 # seconds | |
# wait for button to allow test and click it | |
try: | |
button = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.ui-dialog-buttonpane.ui-widget-content > div > button:nth-child(2)'))) | |
button.click() | |
print ("Test started") | |
delay = 350 # seconds | |
try: | |
WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'testresult-detail'))) | |
print ("Page is ready!") | |
print (browser.current_url) | |
# browser.save_screenshot('screenie.png') | |
# save results as csv | |
#DL-speed | |
downspeed = browser.find_element(By.CSS_SELECTOR,'#verlauf-detail > tbody > tr:nth-child(1) > td:nth-child(3)').text | |
#UL speed | |
upspeed = browser.find_element(By.CSS_SELECTOR,'#verlauf-detail > tbody > tr:nth-child(2) > td:nth-child(3)').text | |
#Ping | |
ping = browser.find_element(By.CSS_SELECTOR,'#verlauf-detail > tbody > tr:nth-child(3) > td:nth-child(3)').text | |
#timestamp | |
timestamp = browser.find_element(By.CSS_SELECTOR,'#testresult-detail > tbody > tr:nth-child(1) > td:nth-child(2) > span').text | |
print ("DL: "+downspeed+" ; UL: "+upspeed+" ; ping: "+ping) | |
fields=[timestamp,downspeed,upspeed,ping,browser.current_url] | |
with open(os.path.join(os.path.dirname(__file__),'results.csv'), 'a') as f: | |
writer = csv.writer(f) | |
writer.writerow(fields) | |
# save cookies | |
#pickle.dump( browser.get_cookies() , open(os.path.join(os.path.dirname(__file__),'cookies.pkl'),"wb")) | |
except TimeoutException: | |
print ("Loading took too much time!") | |
print (browser.current_url) | |
browser.save_screenshot('screenie_timeout.png') | |
except TimeoutException: | |
print ("Didn't find accept button") | |
print (browser.current_url) | |
browser.save_screenshot('screenie_nobutton.png') | |
finally: | |
browser.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment