-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseleniumTools.py
84 lines (80 loc) · 3.02 KB
/
seleniumTools.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
import time
from selenium.webdriver.support.ui import WebDriverWait
def getElementById(elementFrom, timeout, id):
reintentos = 0
element = None
conseguido = False
excepcion = None
while not conseguido and reintentos<3:
try:
element = WebDriverWait(elementFrom, timeout).until(lambda elementFrom: elementFrom.find_element_by_id(id))
conseguido = True
reintentos+=1
except Exception as error:
print "ERROR: Al recuperar un elemento. Reintento: "+str(reintentos)
excepcion = error
reintentos+=1
#Esperamos 2 segundos para ver si le da tiempo al cliente a cargar el elemento que ha fallado en cargar
time.sleep(2)
if not conseguido:
raise excepcion
return element
def getElementByXPath(elementFrom, timeout, XPath):
reintentos = 0
element = None
conseguido = False
excepcion = None
while not conseguido and reintentos<3:
try:
element = WebDriverWait(elementFrom, timeout).until(lambda elementFrom: elementFrom.find_element_by_xpath(XPath))
conseguido = True
reintentos+=1
except Exception as error:
print "ERROR: Al recuperar un elemento. Reintento: "+str(reintentos)
excepcion = error
reintentos+=1
#Esperamos 2 segundos para ver si le da tiempo al cliente a cargar el elemento que ha fallado en cargar
time.sleep(2)
if not conseguido:
raise excepcion
return element
def sendKeysByXPath(elementFrom, timeout, XPath, keys):
reintentos = 0
element = None
conseguido = False
excepcion = None
while not conseguido and reintentos<3:
try:
element = getElementByXPath(elementFrom, timeout, XPath)
element.clear()
element.send_keys(keys)
conseguido = True
reintentos+=1
except Exception as error:
print "ERROR: Al enviar teclas a un elemento. Reintento: "+str(reintentos)
excepcion = error
reintentos+=1
#Esperamos 2 segundos para ver si le da tiempo al cliente a cargar el elemento que ha fallado en cargar
time.sleep(2)
if not conseguido:
raise excepcion
return element
def getElementsByXPath(elementFrom, timeout, XPath):
reintentos = 0
elements = None
conseguido = False
excepcion = None
while not conseguido and reintentos<3:
try:
elements = WebDriverWait(elementFrom, timeout).until(lambda elementFrom: elementFrom.find_elements_by_xpath(XPath))
conseguido = True
reintentos+=1
except Exception as error:
print "ERROR: Al recuperar un elemento. Reintento: "+str(reintentos)
excepcion = error
reintentos+=1
#Esperamos 2 segundos para ver si le da tiempo al cliente a cargar el elemento que ha fallado en cargar
time.sleep(2)
if not conseguido:
raise excepcion
return elements