-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathseleniumHelper.py
195 lines (164 loc) · 5.37 KB
/
seleniumHelper.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
class SeleniumHelper:
driver = None
WAIT = 99999
def loadPage(self, page):
try:
self.driver.get(page)
return True
except:
return False
def submitForm(self, element):
try:
element.submit()
return True
except TimeoutException:
return False
def waitShowElement(self, selector, wait=99999):
try:
wait = WebDriverWait(self.driver, wait)
element = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, selector)))
return element
except:
return None
def waitHideElement(self, selector, wait):
try:
wait = WebDriverWait(self.driver, wait)
element = wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR, selector)))
return element
except:
return None
def getElementFrom(self, fromObject, selector):
try:
return fromObject.find_element_by_css_selector(selector)
except NoSuchElementException:
return None
def getElementsFrom(self, fromObject, selector):
try:
return fromObject.find_elements_by_css_selector(selector)
except NoSuchElementException:
return None
def getElement(self, selector):
return self.getElementFrom(self.driver, selector)
def getElements(self, selector):
return self.getElementsFrom(self.driver, selector)
def getElementFromValue(self, fromObject, selector):
element = self.getElementFrom(fromObject, selector)
return self.getValue(element)
def getElementValue(self, selector):
element = self.getElement(selector)
return self.getValue(element)
def getValue(self, element):
if element:
return element.text
return None
def getAttribute(self, element, attribute):
if element:
return element.get_attribute(attribute)
return None
def getElementFromAttribute(self, fromObject, selector, attribute):
element = self.getElementFrom(fromObject, selector)
return self.getAttribute(element, attribute)
def getElementAttribute(self, selector, attribute):
element = self.getElement(selector)
return self.getAttribute(element, attribute)
def getParentLevels(self, node, levels):
path = '..'
if levels > 1:
for i in range(1, levels):
path = path + '/..'
return node.find_element_by_xpath(path)
def getParentNode(self, node):
return node.find_element_by_xpath('..')
def getChildNodes(self, node):
return node.find_elements_by_xpath('./*')
def selectAndWrite(self, field, value):
fieldObject = self.getElement(field)
fieldObject.send_keys(value)
return fieldObject
def waitAndWrite(self, field, value):
fieldObject = self.waitShowElement(field, self.WAIT)
fieldObject.send_keys(value)
return fieldObject
def pressEnter(self, fieldObject):
fieldObject.send_keys(Keys.RETURN)
return fieldObject
def clickSelector(self, selector):
element = self.getElement(selector)
if element:
try:
actions = webdriver.ActionChains(self.driver)
actions.move_to_element(element)
actions.click(element)
actions.perform()
return True
except:
return False
return False
def click(self, element):
actions = webdriver.ActionChains(self.driver)
actions.move_to_element(element)
actions.click(element)
actions.perform()
def moveToElement(self, element):
self.driver.execute_script("return arguments[0].scrollIntoView();", element)
actions = webdriver.ActionChains(self.driver)
actions.move_to_element(element)
actions.perform()
def scrollDown(self):
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
def scrollingDown(self, times):
for i in range(1, times):
self.scrollDown()
time.sleep(0.5)
def getFieldValue(self, record, parent=None):
exit = None
if parent:
if record['type'] == 'attr':
if record['selector']:
exit = self.getElementFromAttribute(parent, record['selector'], record['attr'])
else:
exit = self.getAttribute(parent)
elif record['type'] == 'text':
if record['selector']:
exit = self.getElementFromValue(parent, record['selector'])
else:
exit = self.getValue(parent)
elif record['type'] == 'style':
exit = record['attr']
else:
if record['type'] == 'attr':
exit = self.getElementAttribute(record['selector'], record['attr'])
elif record['type'] == 'text':
exit = self.getElementValue(record['selector'])
elif record['type'] == 'style':
exit = record['attr']
return exit
def extractSection(self, section):
exit = {}
for subsection in self.SECTIONS[section]:
container = self.SECTIONS[section][subsection]
if container['quantity'] == 'multiple':
exit[subsection] = []
elements = self.getElements(container['selector'])
for element in elements:
row = {}
for field in self.FIELDS[section][subsection]:
record = self.FIELDS[section][subsection][field]
row[field] = self.getFieldValue(record, element)
exit[subsection].append(row)
elif container['quantity'] == 'single':
exit[subsection] = self.getFieldValue(container)
return exit
def saveScreenshoot(self, filePath):
self.driver.save_screenshot(filePath)
def loadAndWait(self, url, selector, wait=9999):
self.loadPage(url)
return self.waitShowElement(selector, wait)
def close(self):
self.driver.quit()