-
Notifications
You must be signed in to change notification settings - Fork 0
/
selen.py
140 lines (131 loc) · 4.33 KB
/
selen.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#m0
#imports
##
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
import json
from pymongo import MongoClient,errors
##
##
DOMAIN = 'domain'
PORT = 27017
host = "mongodb://user:password@domain:27017"
DB_NAME = "altfragen"
client = MongoClient(host)
db = client.altfragen
csv_name = input('name the csv: ')
collection = db[csv_name]
print(client.server_info())
##
#m1
#login
##
driver=webdriver.Chrome("/path/to/driver")
driver.get('https://www.altklausurendb.de/login.php')
time.sleep(2)
driver.find_element(By.XPATH,'//*[@id="login_username"]').send_keys('user')
driver.find_element(By.XPATH,'//*[@id="login_password"]').send_keys('password')
driver.find_element(By.XPATH,'/html/body/div/div/form/fieldset/input[5]').click()
time.sleep(2)
#Clicking on Discussion tab on the menu
driver.find_element(By.XPATH,'//*[@id="nav_top_mobile"]/a[4]').click()
time.sleep(1)
#click on 5th Semster
driver.find_element(By.XPATH,'//*[@id="content"]/div[2]/a[5]').click()
time.sleep(1)
#clicking on module1 (patho)
driver.find_element(By.XPATH,'//*[@id="content"]/div[2]/a[1]').click()
#for loop should begin here
#choosing the year
#***this should be changed every time***#
path_to_year_of_questions = input('Paste the path to the year of the questions: ')
driver.find_element(By.XPATH,path_to_year_of_questions).click()
time.sleep(1)
questions = driver.find_elements(By.XPATH,'//*[@id="content"]/div[2]/a')
driver.find_element(By.XPATH,'//*[@id="content"]/div[2]/a[1]').click()
# get the number of question
num = 1
for question in range(len(questions)):
dic1 = {}
try:
time.sleep(1)
#question drawer
driver.find_element(By.XPATH,'//*[@id="draw_question_header"]').click()
time.sleep(1)
except:
pass
#print question
try:
dic1['frage']=(driver.find_element(By.XPATH,'/html/body/div[3]/div[9]/div[3]/div[2]/div[1]/p[1]').text)
#check if there is a picture
if driver.find_element(By.XPATH,'//*[@id="draw_question_main"]/div/img'):
dic1['frage']= '* '+ dic1['frage']
except:
pass
#print A
try:
dic1['option1']=(driver.find_element(By.XPATH,'//*[@id="draw_question_main"]/p[3]').text)
if dic1['option1'] == '(A)':
dic1['option1']=(driver.find_element(By.XPATH,'//*[@id="draw_question_main"]/p[4]').text)
except:
pass
#print B
try:
dic1['option2']=(driver.find_element(By.XPATH,'//*[@id="draw_question_main"]/p[5]').text)
if dic1['option2'] == '(B)':
dic1['option2']=(driver.find_element(By.XPATH,'//*[@id="draw_question_main"]/p[6]').text)
except:
pass
#print C
try:
dic1['option3']=(driver.find_element(By.XPATH,'//*[@id="draw_question_main"]/p[7]').text)
if dic1['option3'] == '(C)':
dic1['option3']=(driver.find_element(By.XPATH,'//*[@id="draw_question_main"]/p[8]').text)
except:
pass
#print D
try:
dic1['option4']=(driver.find_element(By.XPATH,'//*[@id="draw_question_main"]/p[9]').text)
if dic1['option4'] == '(D)':
dic1['option4']=(driver.find_element(By.XPATH,'//*[@id="draw_question_main"]/p[10]').text)
except:
pass
#print E
try:
dic1['option5']=(driver.find_element(By.XPATH,'//*[@id="draw_question_main"]/p[11]').text)
if dic1['option5'] == '(E)':
dic1['option1']=(driver.find_element(By.XPATH,'//*[@id="draw_question_main"]/p[12]').text)
except:
pass
#print loesung
try:
dic1['ans']=(driver.find_element(By.XPATH,'//*[@id="draw_question_footer"]/p[1]').text)
except:
pass
#print erklaerung
try:
dic1['explain']=(driver.find_element(By.XPATH,'//*[@id="draw_question_footer"]/p[3]').text)
except:
pass
#next question
try:
driver.find_element(By.XPATH,'//*[@id="content"]/div[2]/a[3]').click()
except:
pass
try:
dic1['number']=num
num +=1
except:
pass
# insertion = collection.insert_one(dic1)
try:
result = collection.insert_one(dic1)
except:
#next question
result = collection.insert_one(dic1)
num +=1
time.sleep(1)
driver.find_element(By.XPATH,'//*[@id="content"]/div[2]/a[3]/i').click()
##