iselenium.py
3.47 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
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
from selenium.common.exceptions import TimeoutException
import time
########################################################################
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
#from webdriver_manager.chrome import ChromeDriverManager
class SeleniumInterface:
Chrome = "Chrome"
By = By()
Keys = Keys()
Timeout = 0.5
PageChangeWait = 0.5
def __init__(self, driverType):
self.chrome_options = Options()
self.chrome_options.add_argument("--ignore-certificate-errors")
self.driver = webdriver.Chrome(options=self.chrome_options)
def _getDriver(self, d):
#########QUEDA AL PEDO
if d == SeleniumInterface.Chrome:
return webdriver.Chrome()
raise NoSuchDriver(self.driver)
def get(self, url):
time.sleep(SeleniumInterface.PageChangeWait)
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"):
if element == None:
return None
try:
self._wait(element, (by, lookup))
except TimeoutException:
return None
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 traerTextDiv(self,Id,No="?"):
try:
div = self.driver.find_element(By.ID, Id)
text = div.text
if text:
return text
else:
return No
except Exception as E:
print(f"Error en traerTextDiv {id}, {str(E)}")
return No
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 readInput(self, input):
return input.get_attribute("value")
def readElement(self, element):
return element.get_attribute("innerText")
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"