-
Notifications
You must be signed in to change notification settings - Fork 2
/
selen.py
830 lines (722 loc) · 31.8 KB
/
selen.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
# selen - amazing add-on for selenium framework.
import json
import hashlib
import time
from random import uniform, randint
from collections import Counter
import aiohttp
import asyncio
from aiohttp.client_exceptions import ClientConnectorError
import requests
from selenium import webdriver, common
from selenium.common.exceptions import NoSuchElementException, \
TimeoutException, ElementNotInteractableException, InvalidArgumentException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.chrome.service import Service as OperaService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from webdriver_manager.opera import OperaDriverManager
from seleniumwire import webdriver as SWWD
# from security import COOKIES
# from webdriver_manager.safari import SafariDriverManager
__VERSION = '0.9.15'
COOKIES = []
# Locator variables
ID = "id"
TAG = "tag name"
XPATH = "xpath"
CLASS = "class name"
NAME = "name"
LINK = "link text"
PART_LINK = "partial link text"
CSS = "css selector"
# main tag names locators
l_h1 = (TAG, "h1")
l_h2 = (TAG, "h2")
l_a = (TAG, "a")
l_input = (TAG, "input")
# keys
RETURN = Keys.RETURN
class Selen:
def __init__(self, wd="Chrome", headless=False, url=''):
if wd == "Chrome":
opts = webdriver.ChromeOptions()
# opts.add_argument('--disable-blink-features=AutomationControlled')
if headless:
opts.add_argument('headless')
opts.add_argument('window-size=1600x2600')
opts.add_argument('--disable-blink-features=AutomationControlled')
self.WD = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=opts)
elif wd == "Firefox":
opts = webdriver.FirefoxOptions()
opts.add_argument('--start-maximized')
opts.add_argument('--disable-extensions')
self.WD = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()), options=opts)
elif wd == "Edge":
opts = webdriver.EdgeOptions()
opts.use_chromium = True
opts.binary_location = '/opt/microsoft/msedge/msedge'
opts.add_argument('--start-maximized')
self.WD = webdriver.Edge(service=EdgeService(EdgeChromiumDriverManager().install()), options=opts)
elif wd == "Opera":
opts = webdriver.ChromeOptions()
# opts.binary_location = "/usr/bin/opera"
opts.add_experimental_option('w3c', True)
self.WD = webdriver.Chrome(service=OperaService(OperaDriverManager().install()), options=opts)
self.WD.maximize_window()
elif wd == "Seleniumwire":
opts = SWWD.ChromeOptions()
opts.add_argument('--disable-blink-features=AutomationControlled')
if headless:
opts.add_argument('headless')
opts.add_argument('window-size=1600x2600')
self.WD = SWWD.Chrome(service=ChromeService(ChromeDriverManager().install()), options=opts)
self.WD.maximize_window()
else:
print('!!! WebDriver for: ', wd, " does NOT Exits in the system.")
exit()
self.elems = []
self.elem = WebElement
self.wd_name = wd
self.out_str = self.OutStr('')
self.stat = self.OutDict({})
self.WD.maximize_window()
self.AC = ActionChains(self.WD)
self.WDW = WebDriverWait(self.WD, 10)
self.url = url if url else ""
self.ok_assert = True
self.ok_print = True
self.IS = None
# TODO Class Elem
# TODO Page Source
class OutStr(str):
def print_out(self, message=''):
print(message, self)
return self
class OutDict(dict):
def print_out(self, message=''):
print(message)
print(json.dumps(self, indent=4))
def out(self, message=''):
print(message, "WebElement")
if not self.elem:
print("NO ELEMENTS")
return None
print(self.elem.get_attribute("outerHTML"))
web_elem = {"tag name": self.elem.tag_name,
"abs xpath": self.__xpath_query(),
"visible": self.elem.is_displayed(),
"attributes": self.all_attrs(),
"text": self.elem.text}
print(json.dumps(web_elem, indent=4))
return self
# Print text to STDOUT if it set
def print(self, *args, **kwargs):
first = args[0].upper()
if first != 'OK' and first != 'FAIL' and first != "WARN" and first != "DIV" and self.ok_print:
print(*args, **kwargs)
return
args_str = ", ".join(str(arg) for arg in args[1:])
kwargs_str = ", ".join(f"{key}={value}" for key, value in kwargs.items())
if first == "FAIL":
fro = "\x1b[1m\x1b[31m!!! \x1b[0m"
end = "\x1b[1m\x1b[31m..... FAIL\x1b[0m"
elif first == "OK":
fro = "\x1b[1m\x1b[32m\u2713 \x1b[0m"
end = "\x1b[1m\x1b[32m..... OK\x1b[0m"
elif first == "WARN":
fro = "\x1b[1m\x1b[33m? \x1b[0m"
end = "\x1b[1m\x1b[33m.....WARN \x1b[0m"
elif first == "DIV":
fro = "\x1b[1m\x1b[36m% \x1b[0m"
div_line = "=" * (150 - len(f"{args_str} {kwargs_str}"))
end = f"\x1b[1m\x1b[36m {div_line} \x1b[0m"
else:
return
print(f"{fro} {args_str} {kwargs_str} {end}")
return
def __get_hash(self, elem=None) -> str:
if elem is None:
elem = self.elem
return hashlib.md5(elem.get_attribute("outerHTML").encode()).hexdigest()
def __start(self):
self.elems = self.elem = self.WD
self.IS = None
# Service function Fill elems variables (self.elem, self.elems after operation with WebDriver
def __fill_elems(self, data):
if isinstance(data, list):
self.elems = data
self.elem = self.elems[0] if self.elems else None
else:
self.elem = data
self.elems = [self.elem]
self.WD.execute_script("arguments[0].scrollIntoView();", self.elem)
# Service get depth od tuples in tuples
def __get_tuple_depth(self, t):
if isinstance(t, tuple):
return 1 + max(self.__get_tuple_depth(i) for i in t)
else:
return 0
# Service method to normalize any arguments to tuple of tuples standard
def __args_normalizer(self, *args):
for i in range(self.__get_tuple_depth(args) - 2):
args = sum(args, ())
return args
# Run assertion if it set
def assertion(self, *args):
self.print(*args)
if self.ok_assert:
message = ", ".join(str(arg) for arg in args)
assert False, message
# Delay chain function. Possible to set random delay between two parameters.
def sleep(self, seconds, finish=None):
if finish is None:
self.print("Sleeping for:", seconds, "seconds")
time.sleep(seconds)
return self
delay = uniform(seconds, finish)
self.print("Sleeping for:", delay, "seconds")
time.sleep(uniform(seconds, finish))
return self
# -------- Functions of findings, selecting elements -----------
# The Wait chain function finds and waits for appear element and save elems variables
def Wait(self, *args):
self.__start()
args = self.__args_normalizer(args)
self.print("Wait: Waiting and Looking for :", args)
try:
elems = self.WDW.until(EC.presence_of_all_elements_located(args[0]))
except NoSuchElementException:
self.assertion("FAIL", f"Element not found: {args[0]}")
return
except TimeoutException:
self.assertion("FAIL", f"Element not found, Time out: {args[0]}")
return
self.print("OK", f"Waited and Found Element : {args}")
self.__fill_elems(elems)
if args[1:]:
self.find(*args[1:])
return self
# Find element(s) by arguments for all page elements and from WebDriver directly
def Find(self, *args):
self.__start()
self.find(*args)
return self
# Find element(s) inside other element self.elem by arguments
def find(self, *args):
self.IS = None
args = self.__args_normalizer(args)
for arg in args:
self.__find_one(*arg)
return self
# Service function for the element finding
def __find_one(self, *args: tuple):
if not self.elem:
self.assertion("FAIL", f'Previous element is empty = "{self.elem}". Can not find next {args} element')
return
# trying to find element
try:
elems = self.elem.find_elements(*args[:2])
except NoSuchElementException:
self.assertion("FAIL", f"Element(s) not found: {args}")
return
if len(args) > 2:
new_elems = []
for arg in args[2:]:
if isinstance(arg, int) and 0 <= arg < len(elems):
new_elems.append(elems[arg])
else:
self.print("FAIL", "Wrong index of elements", arg, "maximum is", len(elems))
elems = new_elems
self.__fill_elems(elems)
if self.elem is None:
self.assertion("FAIL", f"Element(s) not found: {args}, elems: {self.elems}, elem: {self.elem}")
self.print("OK", f"Element(s) found: {args}, elems count: {len(self.elems)}")
return
# Find element by tag name, chain function for all page elements and from WebDriver directly
def Tag(self, tag_name: str, *idxs):
self.__start()
self.tag(tag_name, *idxs)
return self
# Find element by tag name inside other elements in self.elems
def tag(self, tag_name: str, *idxs):
self.find((TAG, tag_name, *idxs))
return self
# Find element by Class name, chain function for all page elements and from WebDriver directly
def Cls(self, class_name: str, *idxs):
self.__start()
self.cls(class_name, *idxs)
return self
# Find element by tag name inside other elements in self.elems
def cls(self, class_name: str, *idxs):
self.find((CLASS, class_name, *idxs))
return self
# Find element by Class name, chain function for all page elements and from WebDriver directly
def Xpath(self, query: str, *idxs):
self.__start()
self.xpath(query, *idxs)
return self
# Find element by tag name inside other elements in self.elems
def xpath(self, class_name: str, *idxs):
self.find((XPATH, class_name, *idxs))
return self
def Id(self, id_name: str, *idxs):
self.Find(By.ID, id_name, *idxs)
return self
# Get all image from all page from WebDriver object and optional checking and install
def Img(self, *idxs, check=False):
self.__start()
self.img(*idxs, check=check)
return self
# Get all image from element self.elem and optional checking and extract
def img(self, *idxs, check=False):
self.tag('img', *idxs)
chk = "Checking" if check else ""
self.print("DIV", f"Images Found: {len(self.elems)}. {chk}")
self.stat = self.OutDict({})
for elem in self.elems:
e_hash = self.__get_hash(self.elem)
xpath = self.__xpath_query(elem)
src = elem.get_attribute("src")
alt = elem.get_attribute("alt")
visible = elem.is_displayed()
self.stat[e_hash] = {'xpath': xpath, 'source': src, 'alt': alt, 'visible': visible}
self.print(f"Image: xpath: {xpath}\n source: {src}\n alt = {alt}\n visible: {visible}")
# self.WD.execute_script("arguments[0].style.display = 'block';", image)
if not check:
continue
ok = True
if not src:
ok = False
self.print("FAIL", f" Image without source, xpath: {xpath}")
if not visible:
ok = False
self.print("WARN", f"Invisible Image, xpath: {xpath}")
if not alt:
ok = False
self.print("WARN", f"Image without ALT attribute, xpath: {xpath}")
# checked if loaded
complete = self.WD.execute_script("return arguments[0].complete", elem)
n_width = self.WD.execute_script("return arguments[0].naturalWidth > 0", elem)
if not complete or not n_width:
ok = False
self.print("FAIL", f"Image not loaded, xpath:{xpath}. "
f"Arguments: Complete={complete}, naturalWidth={n_width}")
self.stat[e_hash]['loaded'] = False
else:
self.stat[e_hash]['loaded'] = True
if ok:
self.print("OK", "Image Checked")
self.print("Images :", len(self.stat))
return self
# Selecting Element filter by contain data(text and attributes) from all elements on Page from WD
def Contains(self, data, *idxs):
self.elem = None
self.elems = []
self.contains(data, *idxs)
return self
# Selecting Element filter by contain data(text and attributes) from other element self.elem
def contains(self, data, *idxs):
if not self.__check_data_type(data, str, dict, message="contains()"): return
check_elems = []
if not self.elem:
check_elems = self.WD.find_elements(XPATH, "//*") # Got all elements on page
else:
check_elems.extend(self.elems)
for elem in self.elems:
check_elems.extend(elem.find_elements(CSS, "*"))
result_elems = []
if isinstance(data, dict):
for elem in check_elems:
result = False
for attr, value in data.items():
real_value = elem.get_attribute(attr)
if not real_value:
result = False
continue
if value != real_value:
result = False
continue
result = True
if not result:
continue
result_elems.append(elem)
if isinstance(data, str):
for elem in check_elems:
if elem.text != data:
continue
result_elems.append(elem)
if len(idxs) > 0:
new_elems = []
for idx in idxs[2:]:
if isinstance(idx, int) and 0 <= idx < len(result_elems):
new_elems.append(result_elems[idx])
else:
self.print("FAIL", "Wrong index of elements", idx, "maximum is", len(result_elems))
result_elems = new_elems
self.__fill_elems(result_elems)
self.print("Contains:")
if self.elems:
self.print("OK", f'Found {len(self.elems)} element(s) by "{data}" in {len(check_elems)} element(s)')
else:
self.print("FAIL", f'NOT Found element(s) by "{data}" in {len(check_elems)} element(s)')
return self
# Select parent element of self.elem, can use number of parent level, default = 1
def parent(self, levels=1):
self.IS = None
for i in range(levels):
try:
self.find(XPATH, '..')
except NoSuchElementException:
self.assertion("FAIL", f"Parent Element at level {i + 1} not found")
return self
# -------------- Functions for actions with found element(s) ----------------
# Click chain function have 2 modes simple and with action chains with pause.
def click(self, mode='', action=False, pause=0, random=False):
if random:
idx = randint(0, len(self.elems) - 1)
self.elem = self.elems[idx]
if not mode and not action:
try:
self.elem.click()
self.print("Clicked element:", self.elem)
except ElementNotInteractableException:
self.assertion("FAIL", f"""Cannot Click, try to use ".click(mode='action')", '
f' the Element:"{self.__xpath_query()}""")
elif isinstance(mode, int) or isinstance(mode, float): # click and hold
# self.__hold_click(hold=mode, pause=pause, move=elem)
pass
else:
try:
self.__action_click(mode=mode.lower(), pause=pause)
self.print(mode.upper(), "Action Clicked, element:", self.elem)
except:
self.assertion("FAIL", f"""Cannot Action {mode.upper()} Click the Element:"{self.__xpath_query()}""")
return self
# Service functions for any action clicks
def __action_click(self, mode='', pause=0):
self.AC.move_to_element(self.elem)
if pause > 0:
self.print("Pause before click, seconds:", pause)
self.AC.pause(pause)
mode = '' if mode == 'action' or not mode else mode + '_'
eval(f"self.AC.{mode}click()")
self.AC.perform()
# Display hidden and invisible element
def display(self, elem=None):
elem = self.elem if elem is None else elem
if elem.is_displayed():
self.print("Element VISIBILITY already is ON, xpath:", self.__xpath_query(elem))
else:
self.WD.execute_script("arguments[0].style.display = 'block';", elem)
print("Element VISIBILITY switched ON, xpath:", self.__xpath_query(elem))
return self
# Text of element (self.elem) It has 2 mode text return or check if the text presents
def title(self, title=''):
self.out_str = self.OutStr(self.WD.title)
if title:
self.__checker(self.out_str, title, f'Title "{self.out_str}" at the page: "{self.WD.current_url}"')
return self
return self.out_str
# Current URL of current page
def curr_url(self, url=''):
self.out_str = self.OutStr(self.WD.current_url)
if url:
self.__checker(self.out_str, url, f"Current_URL {self.out_str}")
return self
return self.out_str
# Text of element (self.elem) It has 2 mode text return or check if the text presents
def text(self, text=None):
print(self.elem.text)
self.out_str = self.OutStr(self.elem.text)
# print(self.out_str)
if text is None:
print("text checking", self.out_str.print_out())
return self
self.__checker(self.elem.text, text, f'Text "{self.elem.text}" at element: "{self.__xpath_query()}"')
return self
# Type text in the element (self.elem)
def type(self, *args):
self.out_str = self.OutStr(", ".join(str(arg) for arg in args))
self.click(action=False)
self.elem.clear()
self.elem.send_keys(*args)
return self
def dropdown_select(self, data=None):
tag = self.elem.tag_name
if tag != "select":
self.assertion("FAIL", f"Cannot select from element with tag = <{tag}>, it works with <select>")
return self
if not data: # Get Random option
elems = self.elem.find_elements(By.TAG_NAME, "option")
elem = elems[randint(0, len(elems) - 1)]
data = elem.get_attribute("value")
self.print("OK", f"Random option, value: {data}, text: {elem.text}")
self.__check_data_type(data, int, str, message="dropdown_select")
dropdown = Select(self.elem)
try:
if isinstance(data, str):
dropdown.select_by_value(data)
self.print("OK", f'Selected: "{data}" from dropdown menu: "{self.__xpath_query()}"')
# dropdown.select_by_visible_text(data)
if isinstance(data, int):
dropdown.select_by_index(data)
self.print("OK", f'Selected by Index:: "{data}" from dropdown menu: "{self.__xpath_query()}"')
except NoSuchElementException:
self.assertion("FAIL", f'Cannot find add select by "{data}" from dropdown menu: "{self.__xpath_query()}"')
return self
def dropdown_multiselect(self, *data, random_min=1, random_max=None, from_elems=None):
input_elem = self.elem.find_element(By.TAG_NAME, "input")
self.click()
self.WDW.until(EC.presence_of_all_elements_located((By.TAG_NAME, "li")))
elems = self.elem.find_elements(By.TAG_NAME, "li") if from_elems is None else from_elems
items = [elem.text for elem in elems]
if not data:
max_index = len(elems) - 1
if random_min <= 0 or random_min > max_index:
random_min = 1
if not random_max or random_max > max_index:
random_max = max_index
elif random_max < 1 or random_min > random_max:
random_max = random_min
num = randint(random_min, random_max)
for _ in range(num):
item = items.pop(randint(0, len(items)-1))
input_elem.send_keys(item, Keys.RETURN)
self.print("OK", f"Selected {num} items from {len(elems)} of dropdown list")
else:
for item in data:
if isinstance(item, str) and item in items:
input_elem.send_keys(item, Keys.RETURN)
self.print("OK", "Selected", item)
elif isinstance(item, int) and 0 <= item < len(items):
input_elem.send_keys(items[item], Keys.RETURN)
self.print("OK", f"Selected: {items[item]} by index {item}")
else:
self.assertion("FAIL", f"The item: {item} Not Found in dropdown list or has incorrect type")
input_elem.send_keys(Keys.TAB)
return self
# Print count of selected elements of check if the count of element == asked counts and returns
def count(self, num=None):
count = len(self.elems)
self.out_str = self.Out_str(str(count))
if num is None:
self.print("Count of Elements:", count)
return count
self.__checker(count, num, f"Count of elements: {self.elem}")
return self
# Return absolute xpath of Element or None if not found
def __xpath_query(self, elem=None) -> str or None:
elem = self.elem if elem is None else elem
xpath = self.WD.execute_script("""
var xpath = "";
var containerElem = document.documentElement;
var elem = arguments[0];
while (elem !== containerElem) {
var index = 0;
var sibling = elem.previousSibling;
while (sibling) {
if (sibling.nodeType === Node.ELEMENT_NODE &&
sibling.nodeName.toLowerCase() === elem.nodeName.toLowerCase()) {
index++;
}
sibling = sibling.previousSibling;
}
xpath = "/" + elem.nodeName.toLowerCase() + "[" + (index + 1) + "]" + xpath;
elem = elem.parentNode;
}
return "/" + containerElem.nodeName.toLowerCase() + xpath;
""", elem) # Checking for correct XPATH
try:
self.WD.find_element(XPATH, xpath)
self.out_str = self.OutStr(xpath)
return self.out_str
except NoSuchElementException:
self.assertion("FAIL", f"Built Incorrect abs XPATH, Got {xpath} but Can not to find Element by it")
return self
# Check attribute of self.elem, if exists, for value, if value is None returns value
def attr(self, attr, value=None):
real_value = self.elem.get_attribute(attr)
if real_value is None:
self.assertion("FAIL", f'Attribute "{attr}" not found')
return self
if value is None:
return real_value
self.__checker(real_value, value, f"Attribute: {attr} with value: {value} "
f"for elements: {self.__xpath_query(self.elem)}")
return self
# return all attributes of element
def all_attrs(self, elem=None) -> dict:
elem = self.elem if elem is None else elem
attrs = self.WD.execute_script("""
var items = {};
for (index = 0; index < arguments[0].attributes.length; ++index) {
items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value
};
return items;
""", elem)
return attrs
# -------------- Functions for check any data with found element(s) ----------------
# Service method for compare two parameter and retur
def __checker(self, got, expect, message='') -> bool:
if got == expect:
self.print("OK", message)
# self.output = self.Output("True")
return True
# self.print("FAIL", "Incorrect:", message)
print("* Got data:", got)
print("* Expected:", expect)
self.assertion("FAIL", "Incorrect:", message)
# self.output = self.Output("False")
return False
def __check_data_type(self, data, *args, message=''):
for arg in args:
if isinstance(data, arg):
return True
self.assertion("FAIL", f"{message}: Incorrect method parameters type {type(data)}, it can get: {args} only ")
return False
def get(self, *args, timeout=10):
url = self.url.strip().rstrip("/") + "/"
data = {}
for arg in args[:3]:
if isinstance(arg, str):
url = url + arg.strip().strip("/") + "/"
elif isinstance(arg, dict):
data = arg
self.WD.set_page_load_timeout(timeout)
# Navigate to the web page
if not url:
self.assertion("FAIL", f"NO any url to navigate to any page")
return
try:
self.WD.get(url)
except InvalidArgumentException:
self.assertion('FAIL', f'Incorrect URL: "{url}"')
except TimeoutException:
self.assertion('FAIL', f'Page NOT load, load timed out after {timeout} seconds')
return
self.print("DIV", f'Page "{url}" navigated')
self.curr_url(url)
if data:
self.check_page(data)
def check_page(self, data=None):
self.print("DIV", f'Checking current page {self.WD.current_url}')
if data:
if "wait" in data and data["wait"]:
self.Wait(data["wait"])
if "url" in data and data["url"]:
self.curr_url(data["url"])
if "title" in data and data["title"]:
self.title(data["title"])
self.Img(check=True)
self.check_links()
# --------- Links methods ------------------------------
# Get all links from self.elem page with WebDriver
def check_links(self, asynchron=True):
self.__start()
self.stat = self.Out_dict({})
self.tag('a')
link_hashes = []
self.print("DIV", f'Checking links (href), found: {len(self.elems)}')
for elem in self.elems:
e_hash = self.__get_hash(elem)
stat = self.stat[e_hash] = {'xpath': self.__xpath_query(elem)}
href = elem.get_attribute('href').strip()
if not href:
stat['href'] = None
self.assertion("FAIL", f"No found Link: No attribute 'href', xpath: {stat['xpath']}")
continue
elif href.startswith("mailto") or href.startswith("tel") or href.startswith("%"):
stat['href'] = href
self.print("WARN" f"Found non WEB link {href}")
self.print(json.dumps(stat, indent=4))
continue
stat['href'] = href
link_hashes.append(e_hash)
elem.get_attribute('href')
self.print("Got ", len(link_hashes))
if asynchron:
self.print('Async method using ...')
asyncio.run(self.__check_links_async(link_hashes))
else:
self.print('Sync method using...')
self.__check_links_sync(link_hashes)
return self
# Links response sync checking
def __check_links_sync(self, link_hashes):
for e_hash in link_hashes:
stat = self.stat[e_hash]
try:
response = requests.get(stat['href'])
stat['response_url'] = str(response.url)
stat['code'] = response.status_code
self.__response_stat(e_hash)
except:
stat['response_url'] = "Exception: Unable to reach"
stat['code'] = None
self.__summary_stat()
# Links response async checking
async def __check_links_async(self, link_hashes):
async with aiohttp.ClientSession() as session:
tasks = []
for e_hash in link_hashes:
task = asyncio.create_task(self.__check_link_async(e_hash, session))
tasks.append(task)
responses = await asyncio.gather(*tasks)
for response in responses:
if response:
self.stat[response.e_hash]['response_url'] = str(response.url) if response else None
self.stat[response.e_hash]['code'] = response.status
self.__response_stat(response.e_hash)
self.__summary_stat()
async def __check_link_async(self, e_hash, session):
try:
async with session.get(self.stat[e_hash]['href']) as response:
response.e_hash = e_hash
return response
except ClientConnectorError as err:
print("Error:", err)
self.stat[e_hash]['response_url'] = "Exception: Unable to reach"
self.stat[e_hash]['code'] = None
print("WebElement:", json.dumps(self.stat[e_hash], indent=4))
# self.sleep(50)
def __summary_stat(self):
code_counts = Counter(value.get('code') for value in self.stat.values())
self.print("Checked links:", len(self.stat), ", Status 200 OK is", code_counts[200])
def __response_stat(self, e_hash):
stat = self.stat[e_hash]
code = stat['code']
if code == 200:
self.print("OK", stat['href'], "200")
elif code == 404:
self.assertion("FAIL", f"Broken link found: {stat['href']} in XPATH: {stat['xpath']}")
elif code is None:
self.assertion("FAIL", f"Unable to reach:{stat['href']} in xpath element:{stat['xpath']}")
# --------- Image Methods ---------------------------
# -----------Methods for cookies -----------------
def add_cookies(self):
for cookie in COOKIES[self.wd_name]:
self.WD.execute_cdp_cmd('Network.setCookie', cookie)
# self.WD.add_cookie(cookie)
def save_cookies_to_file(self, file_name):
if self.wd_name in COOKIES:
self.print("Cookies found")
return
COOKIES[self.wd_name] = self.WD.get_cookies()
with open(file_name, 'a') as f:
f.write(f'COOKIES = {COOKIES}\n')
self.print("Cookies saved")
if __name__ == '__main__':
print("Selen - amazing add-on for selenium framework.")
print("Version:", __VERSION)
print("https://github.com/KonstantinSKY/Selen")