iselenium.py
3.6 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.support import expected_conditions as EC
class SeleniumInterface:
Chrome = "Chrome"
By = By()
Keys = Keys()
Timeout = 10
def __init__(self, driverType):
self.driver = self._getDriver(driverType)
def _getDriver(self, d):
if d == SeleniumInterface.Chrome:
return webdriver.Chrome()
raise NoSuchDriver(self.driver)
def get(self, url):
self.driver.get(url)
def find(self, by, lookup, select = "1"):
return self.findFromElement(self.driver, by, lookup, select)
def parent(self, element):
return self.findFromElement(element, SeleniumInterface.By.XPATH, "parent::*")
def children(self, element):
return self.findFromElement(element, SeleniumInterface.By.XPATH, "child::*", "1-")
def findFromElement(self, element, by, lookup, select = "1"):
self._wait(element, (by, lookup))
elements = element.find_elements(by, lookup)
array = []
for selector in select.replace(" ","").split(","):
array += self._resolve(selector, elements)
# One element expected
if "-" not in select and "," not in select:
return array[0]
return array
def write(self, element, keys, clear = True):
if clear:
element.clear()
if type(keys) == list:
for k in keys:
self.write(element, k, clear = False)
element.send_keys(keys)
def _resolve(self, selector, elements):
div, rev, answer = selector.split("-"), False, []
try:
# Not a range
if len(div) == 1:
return [ elements[int(div[0]) - 1] ]
# Range
b = (1 if div[0] == "" else int(div[0]))
e = (len(elements) if div[1] == "" else int(div[1]))
if b > e:
b, e = e, b
rev = True
for i in range(b - 1, e):
if i < 0 or i >= len(elements):
continue
answer.append(elements[i])
if rev:
answer.reverse()
return answer
except:
raise BadInterval(selector)
def _wait(self, fromElement, locator):
Wait(fromElement, SeleniumInterface.Timeout).until(
EC.presence_of_all_elements_located(locator)
)
def __del__(self):
self.driver.close()
class NoSuchDriver(Exception):
def __init__(self, driver):
self.driver = driver
def __str__(self):
return f"The driver '{self.driver}' is not supported"
class BadInterval(Exception):
def __init__(self, interval):
self.interval = interval
def __str__(self):
return f"The interval '{self.interval}' is not valid"
class NotEditable(Exception):
def __init__(self, plate):
self.plate = plate
def __str__(self):
return f"The plate '{self.plate}' is not editable"
if __name__ == "__main__":
plate = "OQY729"
s = SeleniumInterface(SeleniumInterface.Chrome)
### Login ###
s.get("https://rto.cent.gov.ar/rto")
login = s.find(SeleniumInterface.By.NAME, "j_username")
s.write(login, "salvatellih")
psw = s.find(SeleniumInterface.By.NAME, "j_password")
s.write(psw, "3")
button = s.find(SeleniumInterface.By.ID, "submit")
button.click()
### Login ###
### Get to Data ###
s.get("https://rto.cent.gov.ar/rto/RTO/listaDePlanillas")
# children of parent of td with innerText = plate
columns = s.children(s.parent( s.find(SeleniumInterface.By.XPATH, f"//tr//td[text()='{plate}']") ))
# get all a tags and click the last one
options = s.findFromElement(columns[-1], SeleniumInterface.By.TAG_NAME, "a", "1-")
options[-1].click()
# get the 'Datos Técnicos' tag and click it
tec = s.find(SeleniumInterface.By.XPATH, "//a[text()='ir a Datos Técnicos']")
tec.click()
### Get to Data ###
### Read Data ###
### Read Data ###