Skip to content

Instantly share code, notes, and snippets.

@maugern
Last active June 9, 2019 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maugern/48d9597d4c6c0dc1ee8b79408c6bd64f to your computer and use it in GitHub Desktop.
Save maugern/48d9597d4c6c0dc1ee8b79408c6bd64f to your computer and use it in GitHub Desktop.
Fetch your calendar from the Valenciennes university website.
#!/usr/bin/env python3.6
#
# Navigation with selenium on Firefox in the Valenciennes university website.
# Copyright (C) 2018 Nicolas Mauger <nicolas.mauger@etu.univ-valenciennes.fr>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# This program require selenium, gekodriver, datetime and argparse to run.
# usage: Process some authentification in univ-valecenciennes.fr
# [-h] [-d DATE] -u USERNAME -p PASSWORD
#
# optional arguments:
# -h, --help show this help message and exit
#
# optional named arguments:
# -d DATE, --date DATE Specific date of edt - format YYYY-MM-DD. Default:
# today date
#
# required named arguments:
# -u USERNAME, --username USERNAME
# Your Sesame username
# -p PASSWORD, --password PASSWORD
# Your Sesame password
# Reminder : your are not authorized to remove the above license, whatever the reason.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import argparse
import datetime
# Setup the parser
parser = argparse.ArgumentParser("Process some authentification in univ-valecenciennes.fr", add_help="True")
# Define the options :
# The following args are optional
optinalNamed = parser.add_argument_group('optional named arguments')
optinalNamed.add_argument("-d", "--date", help="Specific date of edt - format YYYY-MM-DD. Default: today date")
# The following args are required:
requiredNamed = parser.add_argument_group('required named arguments')
requiredNamed.add_argument("-u", "--username", help="Your Sesame username", required=True)
requiredNamed.add_argument("-p", "--password", help="Your Sesame password", required=True)
args = parser.parse_args()
# If the date is not set, set the current date
if args.date:
# Rise an exception if the dateformat is not respected
try:
year, month, day = map(int, args.date.split('-'))
edt_date = datetime.date(year, month, day)
except ValueError:
msg = "Not a valid date: {}. Should be like : YYYY-mm-dd".format(args.date)
raise argparse.ArgumentTypeError(msg)
else:
edt_date = datetime.date.today()
#Define all xpaths for all DOM elements who will be use during the navigaton
xpaths = { 'usernameTxtBox' : "//input[@name='username']",
'passwordTxtBox' : "//input[@name='password']",
'submitButton' : "//input[@name='submit']",
'mesCoursButton' : "//a[@title='Mes cours']",
'ExportButton' : "//a[@title='Export Pdf']"
}
mydriver = webdriver.Firefox()
mydriver.get("https://cas.univ-valenciennes.fr/cas/login?service=https://portail.univ-valenciennes.fr/Login")
mydriver.maximize_window()
#Clear Username TextBox if already allowed "Remember Me"
mydriver.find_element_by_xpath(xpaths['usernameTxtBox']).clear()
#Write Username in Username TextBox
mydriver.find_element_by_xpath(xpaths['usernameTxtBox']).send_keys(args.username)
#Clear Password TextBox if already allowed "Remember Me"
mydriver.find_element_by_xpath(xpaths['passwordTxtBox']).clear()
#Write Password in password TextBox
mydriver.find_element_by_xpath(xpaths['passwordTxtBox']).send_keys(args.password)
#Click Login button
mydriver.find_element_by_xpath(xpaths['submitButton']).click()
#Go to "EDT" link
mydriver.get('https://vtmob.univ-valenciennes.fr/esup-vtclient-up4/stylesheets/desktop/welcome.xhtml')
#Go to correct date with the implemented JS fonction.
mydriver.execute_script("document.getElementById('formCal:date').value='"+edt_date.strftime('%d/%m/%Y')+"';document.forms['formCal'].submit();")
#Click Export button, who download the pdf.
#The download action may be before page display, but the pdf
#remains the correct one.
mydriver.find_element_by_xpath(xpaths['ExportButton']).click()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment