-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathelements.py
302 lines (209 loc) · 8.7 KB
/
elements.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/python3
# -*- encoding=utf8 -*-
import time
from termcolor import colored
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
class WebElement(object):
_locator = ('', '')
_web_driver = None
_page = None
_timeout = 10
_wait_after_click = False # TODO: how we can wait after click?
def __init__(self, timeout=10, wait_after_click=False, **kwargs):
self._timeout = timeout
self._wait_after_click = wait_after_click
for attr in kwargs:
self._locator = (str(attr).replace('_', ' '), str(kwargs.get(attr)))
def find(self, timeout=10):
""" Find element on the page. """
element = None
try:
element = WebDriverWait(self._web_driver, timeout).until(
EC.presence_of_element_located(self._locator)
)
except:
print(colored('Element not found on the page!', 'red'))
return element
def wait_to_be_clickable(self, timeout=10, check_visibility=True):
""" Wait until the element will be ready for click. """
element = None
try:
element = WebDriverWait(self._web_driver, timeout).until(
EC.element_to_be_clickable(self._locator)
)
except:
print(colored('Element not clickable!', 'red'))
if check_visibility:
self.wait_until_not_visible()
return element
def is_clickable(self):
""" Check is element ready for click or not. """
element = self.wait_to_be_clickable(timeout=0.1)
return element is not None
def is_presented(self):
""" Check that element is presented on the page. """
element = self.find(timeout=0.1)
return element is not None
def is_visible(self):
""" Check is the element visible or not. """
element = self.find(timeout=0.1)
if element:
return element.is_displayed()
return False
def wait_until_not_visible(self, timeout=10):
element = None
try:
element = WebDriverWait(self._web_driver, timeout).until(
EC.visibility_of_element_located(self._locator)
)
except:
print(colored('Element not visible!', 'red'))
if element:
js = ('return (!(arguments[0].offsetParent === null) && '
'!(window.getComputedStyle(arguments[0]) === "none") &&'
'arguments[0].offsetWidth > 0 && arguments[0].offsetHeight > 0'
');')
visibility = self._web_driver.execute_script(js, element)
iteration = 0
while not visibility and iteration < 10:
time.sleep(0.5)
iteration += 1
visibility = self._web_driver.execute_script(js, element)
print('Element {0} visibility: {1}'.format(self._locator, visibility))
return element
def send_keys(self, keys, wait=2):
""" Send keys to the element. """
keys = keys.replace('\n', '\ue007')
element = self.find()
if element:
element.click()
element.clear()
element.send_keys(keys)
time.sleep(wait)
else:
msg = 'Element with locator {0} not found'
raise AttributeError(msg.format(self._locator))
def get_text(self):
""" Get text of the element. """
element = self.find()
text = ''
try:
text = str(element.text)
except Exception as e:
print('Error: {0}'.format(e))
return text
def get_attribute(self, attr_name):
""" Get attribute of the element. """
element = self.find()
if element:
return element.get_attribute(attr_name)
def _set_value(self, web_driver, value, clear=True):
""" Set value to the input element. """
element = self.find()
if clear:
element.clear()
element.send_keys(value)
def click(self, hold_seconds=0, x_offset=1, y_offset=1):
""" Wait and click the element. """
element = self.wait_to_be_clickable()
if element:
action = ActionChains(self._web_driver)
action.move_to_element_with_offset(element, x_offset, y_offset).\
pause(hold_seconds).click(on_element=element).perform()
else:
msg = 'Element with locator {0} not found'
raise AttributeError(msg.format(self._locator))
if self._wait_after_click:
self._page.wait_page_loaded()
def right_mouse_click(self, x_offset=0, y_offset=0, hold_seconds=0):
""" Click right mouse button on the element. """
element = self.wait_to_be_clickable()
if element:
action = ActionChains(self._web_driver)
action.move_to_element_with_offset(element, x_offset, y_offset). \
pause(hold_seconds).context_click(on_element=element).perform()
else:
msg = 'Element with locator {0} not found'
raise AttributeError(msg.format(self._locator))
def highlight_and_make_screenshot(self, file_name='element.png'):
""" Highlight element and make the screen-shot of all page. """
element = self.find()
# Scroll page to the element:
self._web_driver.execute_script("arguments[0].scrollIntoView();", element)
# Add red border to the style:
self._web_driver.execute_script("arguments[0].style.border='3px solid red'", element)
# Make screen-shot of the page:
self._web_driver.save_screenshot(file_name)
def scroll_to_element(self):
""" Scroll page to the element. """
element = self.find()
# Scroll page to the element:
# Option #1 to scroll to element:
# self._web_driver.execute_script("arguments[0].scrollIntoView();", element)
# Option #2 to scroll to element:
try:
element.send_keys(Keys.DOWN)
except Exception as e:
pass # Just ignore the error if we can't send the keys to the element
def delete(self):
""" Deletes element from the page. """
element = self.find()
# Delete element:
self._web_driver.execute_script("arguments[0].remove();", element)
class ManyWebElements(WebElement):
def __getitem__(self, item):
""" Get list of elements and try to return required element. """
elements = self.find()
return elements[item]
def find(self, timeout=10):
""" Find elements on the page. """
elements = []
try:
elements = WebDriverWait(self._web_driver, timeout).until(
EC.presence_of_all_elements_located(self._locator)
)
except:
print(colored('Elements not found on the page!', 'red'))
return elements
def _set_value(self, web_driver, value):
""" Note: this action is not applicable for the list of elements. """
raise NotImplemented('This action is not applicable for the list of elements')
def click(self, hold_seconds=0, x_offset=0, y_offset=0):
""" Note: this action is not applicable for the list of elements. """
raise NotImplemented('This action is not applicable for the list of elements')
def count(self):
""" Get count of elements. """
elements = self.find()
return len(elements)
def get_text(self):
""" Get text of elements. """
elements = self.find()
result = []
for element in elements:
text = ''
try:
text = str(element.text)
except Exception as e:
print('Error: {0}'.format(e))
result.append(text)
return result
def get_attribute(self, attr_name):
""" Get attribute of all elements. """
results = []
elements = self.find()
for element in elements:
results.append(element.get_attribute(attr_name))
return results
def highlight_and_make_screenshot(self, file_name='element.png'):
""" Highlight elements and make the screen-shot of all page. """
elements = self.find()
for element in elements:
# Scroll page to the element:
self._web_driver.execute_script("arguments[0].scrollIntoView();", element)
# Add red border to the style:
self._web_driver.execute_script("arguments[0].style.border='3px solid red'", element)
# Make screen-shot of the page:
self._web_driver.save_screenshot(file_name)