-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.py
113 lines (95 loc) · 4.09 KB
/
web.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import WebDriverException
from xvfbwrapper import Xvfb
from bs4 import BeautifulSoup
def available_curses(carne, passw, visible=False, close=True):
"""
Enters ematricula.ucr.ac.cr and then
goes to the section that has the information
about available careers.
Example usage:
Launch a invisible browser and get the curses.
-> available_curses('b53777', 'mypassword')
Launch a visible browser to see step by step what is done.
-> available_curses('b53777', 'mypassword', visible=True)
Launch a visible browser and don't close when finished.
-> available_curses('b53777', 'mypassword', visible=True, close=False)
"""
# Create and start a virtualdisplay
vdisplay = Xvfb()
# Launch the display if visible is false
if not visible:
vdisplay.start()
# Open the browser and does the magic
driver = webdriver.Firefox()
driver.get('https://ematricula.ucr.ac.cr/ematricula/login.do')
carne_box = driver.find_element_by_name('carne')
pass_box = driver.find_element_by_name('pin')
carne_box.send_keys(carne)
pass_box.send_keys(passw)
driver.find_element_by_name('crudMethod').click()
wait_until_title_contains(driver, 'Sistema eMatricula')
driver.find_element_by_link_text('Cursos Pendientes del Plan').click()
wait_until_element_is_located(driver, 'formCarreras')
career_dropdown = driver.find_element_by_name("carrera")
careers = [x for x in career_dropdown.find_elements_by_tag_name('option')[1:]]
for each_career in careers:
print(each_career.get_attribute('innerHTML'))
print(each_career.get_attribute('value'))
each_career.click()
wait_until_class_is_located(driver,'data')
#courses_dropdown=driver.find_element_by_xpath("/html/body/div[@id='all']/div[@id='ct']/table[@class='data']/tbody/tr[2]/td[@class='row2'][2]")
#courses_dropdown=driver.find_element_by_tag_name('tr')
courses_dropdown= driver.find_element_by_xpath("/html/body/div[@id='all']/div[@id='ct']/table[@class='data']/tbody/tr/*")
print("Here i am")
courses=[x for x in courses_dropdown.find_elements_by_tag_name('td')]
for each_course in courses:
print(each_course.get_attrubute('class'))
# Quit the browser
if close:
driver.quit()
# If not visible, stop display
if not visible:
vdisplay.stop()
def wait_until_title_contains(driver, piece, timeout=10):
"""
Wait until the title contains the piece.
Default timeout will be 10 seconds.
"""
try:
# Wait until title contains...
element = WebDriverWait(driver, timeout).until(
EC.title_contains(piece)
)
except TimeoutException:
raise WebDriverException("It appears that there's someone logged in already...")
finally:
# Log succesfully
print("Loaded page that contains '"+piece+"' succesfully...")
def wait_until_element_is_located(driver, element_id, timeout=10):
"""
Wait until the element is located in the driver.
Default timeout will be 10 seconds.
"""
try:
element = WebDriverWait(driver, timeout).until(
EC.presence_of_element_located((By.ID, element_id))
)
finally:
print("Loaded page that contains '"+element_id+"' succesfully...")
def wait_until_class_is_located(driver, element_class, timeout=10):
"""
Wait until the element is located in the driver.
Default timeout will be 10 seconds.
"""
try:
element = WebDriverWait(driver, timeout).until(
EC.presence_of_element_located((By.CLASS_NAME, element_class))
)
finally:
print("Loaded page that contains '"+element_class+"' succesfully...")