-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
1025 lines (917 loc) · 42 KB
/
index.js
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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const tools = require('os-tools');
const self = module.exports = {
/**
* Will create a new chromium browser
*
* @param url -> the url to log in to
* @param slowMo -> how long to wait before each command
* @param headless -> show/hide browser
* @param width -> browser width
* @param height -> browser height
*/
createBrowser: async function (url = "about:blank",
slowMo = 5,
headless = false,
width = 1300,
height = 768) {
const browser = await require('puppeteer').launch({
headless: headless,
slowMo: slowMo // slow down by 5
});
const page = await browser.newPage();
await page.setViewport({width: width, height: height});
await self.navigateTo(page, url);
return [browser, page]
},
/**
* Will create a new firefox browser. You should use this browser if you're planning to register to Google websites.
* NOTICE: you will need to install FireFox Nightly and supply your firefox execuatble file.
*
* @param url the url to log in to
* @param slowMo how long to wait before each command
* @param headless show/hide browser
* @param width browser width
* @param height browser height
* @param fireFoxNightlyPath the path to your FireFox Nightly runner file.
* In Windows that's usually your firefox.exe file (like 'C:/Program Files/Firefox Nightly/firefox.exe').
* In Mac that's usually your firefox file, located in your Firefox Nightly.app, inside the Applications dir.
*/
createFirefoxBrowser: async function (url = "about:blank",
slowMo = 5,
headless = false,
width = 1300,
height = 768,
fireFoxNightlyPath = '/Applications/Firefox Nightly.app/Contents/MacOS/firefox') {
const browser = await require('puppeteer').launch({
headless: headless,
product: 'firefox',
args: [
'-wait-for-browser'
],
executablePath: fireFoxNightlyPath,
// userDataDir: 'C:/Users/Milen/Desktop/Puppeteer-Test_Firefox/Profile_FirefoxNightly',
slowMo: slowMo // slow down by 5
});
const page = await browser.newPage();
await page.setViewport({width: width, height: height});
await self.navigateTo(page, url);
return [browser, page];
},
/**
* Will navigate to a certain url with an option to search for a selector after page load
*/
navigateTo: async function (page,
url,
givenTimeout = null,
waitForSelector = null,
timeoutSelectorSearch = null,
delayAfterSelector = 1500,
delayAfterNavigated = 0) {
await page.goto(url, {waitUntil: 'load', timeout: givenTimeout});
await tools.delay(delayAfterNavigated);
if (waitForSelector != null) {
await self.waitForSelector(page, waitForSelector, timeoutSelectorSearch, delayAfterSelector)
}
},
/**
* Will wait for an element to appear.
*
* @param page the puppeteer page
* @param selector the selector to search for
* @param timeout 0 to disable timeout
* @param delayAfterFound how long to wait after found
* @return ElementHandle the element, if found, else, undefined
*/
waitForSelector: async function (page, selector, timeout = null, delayAfterFound = 1500) {
let ele = undefined;
try {
ele = await page.waitForSelector(selector, {timeout: timeout});
} catch {
}
await tools.delay(delayAfterFound);
return ele
},
/**
* Will wait for an element with a text to appear.
*
* @param page the puppeteer page
* @param selector the selector to search for
* @param text the text you wait for to appear
* @param timeout optional timeout
* @param checkEach tracker search time
* @param delayAfterFound the delay after found
* @param caseSensitive true for exact text match false, ignore capitals etc...
* @param includes set to true if the element contains text
* @param identical set to true if element has the EXACT text ( === )
* @param innerHTML set to true for innerHTML. False for innerText
* @return elmentHandle the element if found, else undefined
*/
waitForSelectorWithText: async function (page,
selector,
text,
checkEach = 1000,
caseSensitive = false,
includes = true,
identical = false,
innerHTML = true,
timeout = null,
delayAfterFound = 0) {
let initialTime = new Date().getTime();
let futureTime = null;
if (timeout !== null) {
futureTime = initialTime + timeout;
}
if (!caseSensitive) {
text = text.toLowerCase();
}
while (true) {
if (futureTime !== null && new Date().getTime() >= futureTime) {
console.log("times up! returning");
return undefined
}
let found = await self.waitForSelector(page, selector, 1000);
if (found) {
let ele = undefined;
console.log('searching for text');
ele = await self.getElementByText(page, selector, text, caseSensitive, includes, identical, innerHTML);
if (ele !== undefined) {
console.log("found it!");
await tools.delay(delayAfterFound);
return ele
}
console.log("didn't found yet!");
await tools.delay(checkEach);
}
}
},
/**
* Will wait for an element to be removed from the dom
*
* @param page the page
* @param selector the selector to be removed
* @param checkEach search every x millis for the selector
* @param disappearFor the selector should disappear for x millis
*/
waitForSelectorToBeRemoved: async function (page, selector, checkEach = 2000, disappearFor = 1000) {
while (true) {
let found = await self.waitForSelector(page, selector, disappearFor, 0);
if (!found) {
return
}
await tools.delay(checkEach);
}
},
/**
* Will wait for elements to appear
*/
waitForSelectors: async function (page, timeout = null, delayAfterFound = 1500, ...selectors) {
await page.waitForSelector(selectors, {timeout: timeout});
await tools.delay(delayAfterFound)
},
/**
* Will find an element by it's partial attribute value
*/
getElementByPartialAttributeValue: async function (page, eleTag, eleAttributeName, partialAttributeValue, findMoreThanOneElement = false) {
if (findMoreThanOneElement) {
return await self.getElementsBySelector(page, eleTag + '[' + eleAttributeName + "~='" + partialAttributeValue + "']")
}
return await self.getElementBySelector(page, eleTag + '[' + eleAttributeName + "~='" + partialAttributeValue + "']")
},
/**
* Will find an element by it's attribute value suffix
*/
getElementBySuffixAttributeValue: async function (page, eleTag, eleAttributeName, attributeSuffixValue, findMoreThanOneElement = false) {
if (findMoreThanOneElement) {
return await self.getElementsBySelector(page, eleTag + '[' + eleAttributeName + "$='" + attributeSuffixValue + "']")
}
return await self.getElementBySelector(page, eleTag + '[' + eleAttributeName + "$='" + attributeSuffixValue + "']")
},
/**
* Will find an element by it's attribute value prefix
*/
getElementByPrefixAttributeValue: async function (page, eleTag, eleAttributeName, attributePrefixValue, findMoreThanOneElement = false) {
if (findMoreThanOneElement) {
return await self.getElementsBySelector(page, eleTag + '[' + eleAttributeName + "^='" + attributePrefixValue + "']")
}
return await self.getElementBySelector(page, eleTag + '[' + eleAttributeName + "^='" + attributePrefixValue + "']")
},
/**
* Will wait for navigation to change
*/
waitForNavigation: async function (page, selector, timeout = null) {
return await page.waitForNavigation(selector, {timeout: timeout});
},
/**
* Will set text to a selector
*
* @param page the current page
* @param selector the selector to write upon. For example: input[id="username"]
* @param text the text you wish to write
* @param delayAfter the delay after the type
* @param typeDelay the delay between each written letter
* @param clearTextBefore set to true if you want to clear the text box before (manual clear)
*/
setTextToSelector: async function (page, selector, text, delayAfter = 0, typeDelay = 20, clearTextBefore = true) {
await mSetText(page, null, selector, text, delayAfter, typeDelay, clearTextBefore)
},
/**
* Will set text to xpath
*
* @param page the current page
* @param element optional element you want to write upon
* @param text the text you wish to write
* @param delayAfter the delay after the type
* @param typeDelay the delay between each written letter
* @param clearTextBefore set to true if you want to clear the text box before (manual clear)
*/
setTextToXpath: async function (page, xpath, text, delayAfter = 0, typeDelay = 20, clearTextBefore = true) {
let element = await self.getElementByXPath(page, xpath)
await mSetText(page, element, null, text, delayAfter, typeDelay, clearTextBefore)
},
/**
* Will set text to an element
*
* @param page the current page
* @param element optional element you want to write upon
* @param text the text you wish to write
* @param delayAfter the delay after the type
* @param typeDelay the delay between each written letter
* @param clearTextBefore set to true if you want to clear the text box before (manual clear)
*/
setTextToElement: async function (page, element, text, delayAfter = 0, typeDelay = 20, clearTextBefore = true) {
await mSetText(page, element, null, text, delayAfter, typeDelay, clearTextBefore)
},
/**
* Will type alpha/numerical on the keyboard
*
* @param page the current page
* @param text the text to write
* @param delay the delay between letters
* @param delayAfter the delay after the whole type
*/
typeOnKeyboard: async function (page, text, delay = 2, delayAfter = 0) {
await page.keyboard.type(text, {delay: delay});
await tools.delay(delayAfter)
},
/**
* Will read text from selector
*
* @param page the current page
* @param selector the selector to read
* @param innerText true for innerText, false for innerHTML
* @param delayAfter the delay after the whole type
*/
readTextFromSelector: async function (page, selector, innerText = true, delayAfter = 0) {
var property = innerText
if (!innerText) {
property = 'innerHTML'
}
const element = await page.$(selector);
const text = await (await element.getProperty(property)).jsonValue();
await tools.delay(delayAfter);
return text
},
/**
* Will click on a selector
*
* @param page the current page
* @param selector the selector to click. For example: a:nth-of-type(2)
* @param delayAfterClick optional delay after click
* @param selectorToFindAfterClick optional element to look for after click
* @param howLongToWaitForSelector optional time to look for the element after click
* @param delayAfterSelectorFound optional delay after element found
*/
clickOnSelector: async function (page,
selector,
delayAfterClick = 0,
selectorToFindAfterClick = null,
howLongToWaitForSelector = null,
delayAfterSelectorFound = 1500) {
await mClick(page, selector, null, delayAfterClick, selectorToFindAfterClick, howLongToWaitForSelector, delayAfterSelectorFound)
},
/**
* Will click on a XPath selector
*
* @param page the current page
* @param xpath the xpath to click. For example: /html/body/div[6]/div[1]/div[1]/h1
* @param delayAfterClick optional delay after click
* @param selectorToFindAfterClick optional element to look for after click
* @param howLongToWaitForSelector optional time to look for the element after click
* @param delayAfterSelectorFound optional delay after element found
*/
clickOnXPath: async function (page,
xpath,
delayAfterClick = 0,
selectorToFindAfterClick = null,
howLongToWaitForSelector = null,
delayAfterSelectorFound = 1500) {
let xpathEle = await self.getElementByXPath(page, xpath)
await mClick(page, null, xpathEle, delayAfterClick, selectorToFindAfterClick, howLongToWaitForSelector, delayAfterSelectorFound)
},
/**
* Will mouse click on an xpath
*
* @param page the current page
* @param xpath the xpath to click
* @param selector the selector to click. For example: a:nth-of-type(2)
* @param mouseBtn the mouse btn to click ('left'/'right')
* @param delayAfterClick optional delay after click
*/
mouseClickOnXPath: async function (page,
xpath,
mouseBtn = 'left',
delayAfterClick = 0) {
const element = await self.getElementByXPath(page, xpath)
await self.mouseClickOnElement(element, mouseBtn, delayAfterClick)
},
/**
* Will mouse click on a selector
*
* @param page the current page
* @param selector the selector to click. For example: a:nth-of-type(2)
* @param mouseBtn the mouse btn to click ('left'/'right')
* @param delayAfterClick optional delay after click
*/
mouseClickOnSelector: async function (page,
selector,
mouseBtn = 'left',
delayAfterClick = 0) {
const element = await self.getElementBySelector(page, selector)
await self.mouseClickOnElement(element, mouseBtn, delayAfterClick)
},
/**
* Will mouse click on a selector which contains text
*
* @param page the puppeteer page
* @param selector the selector to click. For example: 'div'
* @param text the element's innerText
* @param caseSensitive toggle this to find the exact text or to ignore higher/lower cases
* @param includes set to true if to click on an element based on contains() text
* @param identical set to true if to click on an element which has the EXACT text ( === )
* @param innerHTML set to true for innerHTML. False for innerText
* @param mouseBtn the mouse btn to click ('left'/'right')
* @param delayAfterClick optional delay after click
* @param selectorToFindAfterClick optional element to look for after click
* @param howLongToWaitForSelector optional time to look for the element after click
* @param delayAfterSelectorFound optional delay after element found
*/
mouseClickOnSelectorContainsText: async function (page,
selector,
text,
caseSensitive = false,
includes = true,
identical = false,
innerHTML = true,
mouseBtn = 'left',
delayAfterClick = 0,
selectorToFindAfterClick = null,
howLongToWaitForSelector = null,
delayAfterSelectorFound = 1500) {
var element = await self.getElementByText(page, selector, text, caseSensitive, includes, identical, innerHTML);
await self.mouseClickOnElement(element, mouseBtn, delayAfterClick)
},
/**
* Will mouse click on element
*
* @param element the element to click
* @param mouseBtn the mouse btn to click ('left'/'right')
* @param delayAfterClick optional delay after click
*/
mouseClickOnElement: async function (element,
mouseBtn = 'left',
delayAfterClick = 0) {
await element.click({
button: mouseBtn
});
await tools.delay(delayAfterClick)
}
,
/**
* Will click an element/selector
*
* @param page the current page
* @param element the element to click upon
* @param delayAfterClick optional delay after click
* @param selectorToFindAfterClick optional element to look for after click
* @param howLongToWaitForSelector optional time to look for the element after click
* @param delayAfterSelectorFound optional delay after element found
*/
clickOnElement: async function (page,
element,
delayAfterClick = 0,
selectorToFindAfterClick = null,
howLongToWaitForSelector = null,
delayAfterSelectorFound = 1500) {
await mClick(page, null, element, delayAfterClick, selectorToFindAfterClick, howLongToWaitForSelector, delayAfterSelectorFound)
}
,
/**
* Will selectByValue an element based on it's value
* For more formidable approach, look for CheerioHelper.isSelectHasValue()
*/
selectByValue: async function (page, selector, value, delayAfter = 0) {
await page.select(selector, value);
await tools.delay(delayAfter)
}
,
/**
* Will clear text from selector or element
*/
clearText: async function (page, selector, element = null, delayAfter = 0) {
// await page.evaluate(function (selector) {
// // this code has now has access to foo
// document.querySelector(selector).value = "";
// }, selector);
let ele = element;
if (ele === null) {
ele = await self.getElementBySelector(page, selector)
}
await ele.click({clickCount: 3});
await ele.type("");
await tools.delay(delayAfter)
}
,
/**
* Will create a count down (10 9 8 ...)
*/
makeCountDown: async function (from) {
for (let i = from; i > 0; i--) {
console.log(i);
await tools.delay(1000)
}
}
,
/**
* Will download a file.
* NOTICE: if you only want to change the saving location of a file, call setDownloadedFilesLocation(output).
*
* @param page the puppeteer page
* @param outputPath the path to the downloaded file
* @param downloadSelector the selector to click in order to start the download
*/
downloadFile: async function (page, outputPath, downloadSelector) {
self.setDownloadedFilesLocation(page, outputPath);
await self.clickOnSelector(page, downloadSelector)
}
,
/**
* will set the location to which the downloaded files will be saved
*/
setDownloadedFilesLocation: async function (page, outputPath) {
await page._client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath: outputPath
});
}
,
/**
* will scroll a selector into view
*/
scrollSelectorIntoView: async function (page, selector) {
let element = await self.getElementBySelector(page, selector)
await self.scrollElementIntoView(page, element)
}
,
/**
* will scroll an xpath into view
*/
scrollXPathIntoView: async function (page, xpath) {
let element = await self.getElementByXPath(page, xpath)
await self.scrollElementIntoView(page, element)
}
,
/**
* will scroll an element into view
*/
scrollElementIntoView: async function (page, element) {
await page.evaluateHandle((element) => {
element.scrollIntoView()
}, element);
}
,
/**
* Will click on an element which contains text
* for example("div", "Floki") will click on a div with an innerText of "Floki" (uppercase sensitive)
*
* @param page the puppeteer page
* @param selector the selector to click. For example: a:nth-of-type(2)
* @param text the element's innerText
* @param caseSensitive toggle this to find the exact text or to ignore higher/lower cases
* @param includes set to true if to click on an element based on contains() text
* @param identical set to true if to click on an element which has the EXACT text ( === )
* @param innerHTML set to true for innerHTML. False for innerText
* @param delayAfterClick optional delay after click
* @param selectorToFindAfterClick optional element to look for after click
* @param howLongToWaitForSelector optional time to look for the element after click
* @param delayAfterSelectorFound optional delay after element found
*/
clickOnElementContainsText: async function (page,
selector,
text,
caseSensitive = false,
includes = true,
identical = false,
innerHTML = true,
delayAfterClick = 0,
selectorToFindAfterClick = null,
howLongToWaitForSelector = null,
delayAfterSelectorFound = 1500) {
var ele = await self.getElementByText(page, selector, text, caseSensitive, includes, identical, innerHTML);
await self.clickOnElement(page, ele, delayAfterClick, selectorToFindAfterClick, howLongToWaitForSelector, delayAfterSelectorFound)
}
,
/**
* Will return an element contains text
* for example("div", "Floki") will click on a div with an innerText of "Floki" (uppercase sensitive)
*
* @param page the puppeteer page
* @param selector the selector to find. For example: a:nth-of-type(2)
* @param text the element's innerText
* @param caseSensitive toggle this to find the exact text or to ignore higher/lower cases
* @param includes set to true if the element contains text
* @param identical set to true if element has the EXACT text ( === )
* @param innerHTML set to true for innerHTML. False for innerText
*/
getElementByText: async function (page,
selector,
text,
caseSensitive = false,
includes = true,
identical = false,
innerHTML = true) {
let ele = undefined;
try {
ele = await page.evaluateHandle((selector, text, caseSensitive, includes, identical, innerHTML) => {
// this code has now has access to foo
let allEle = document.querySelectorAll(selector);
if (!caseSensitive) {
text = text.toLowerCase()
}
for (let i = 0; i < allEle.length; i++) {
let eleText = ''
if (innerHTML) {
eleText = allEle[i].innerHTML;
} else {
eleText = allEle[i].innerText;
}
if (!caseSensitive) {
eleText = eleText.toLowerCase()
}
if (includes) {
if (eleText.includes(text)) {
return allEle[i]
}
}
if (identical) {
if (eleText === text) {
return allEle[i]
}
}
}
return undefined
}, selector, text, caseSensitive, includes, identical, innerHTML);
} catch (e) {
await self.getElementByText(page, selector, text, caseSensitive, includes, identical);
return
}
return ele.type === undefined ? undefined : ele
}
,
/**
* Will return element/s from the dom
*/
getElementsBySelector: async function (page, selector) {
return await page.$$(selector);
}
,
/**
* Will return element from the dom/other element.
* If no found, return undefined
*/
getElementBySelector: async function (pageOrElement, selector) {
try {
return await pageOrElement.$(selector);
} catch (e) {
return undefined
}
}
,
/**
* Will return element from the dom/other element.
* If no found, return undefined
*/
getElementByXPath: async function (pageOrElement, xpath) {
try {
return await pageOrElement.$x(xpath);
} catch (e) {
return undefined
}
}
,
/**
* Will return the next sibling element from another element
*/
getNextSibling: async function (page, element) {
let ele = await page.evaluateHandle(el => el.nextElementSibling, element);
return ele.type === undefined ? null : ele
}
,
/**
* Will return the previous sibling element from another element
*/
getPreviousSibling: async function (page, element) {
let ele = await page.evaluateHandle(el => el.previousElementSibling, element);
return ele.type === undefined ? null : ele
}
,
/**
* Will return the parent of an element
*/
getParent: async function (page, element) {
let ele = await page.evaluateHandle(el => el.parentElement, element);
return ele.type === undefined ? null : ele
}
,
/**
* Will return the sibling of an element
*/
getOnlyDirectChildren: async function (page, element) {
return page.evaluateHandle(el => el.children, element)
}
,
/**
* Will return the inner html of an element
*/
getInnerHTML: async function (page, element) {
return await page.evaluate(e => e.innerHTML, element);
}
,
/**
* Will return the inner text of an element
*/
getInnerText: async function (page, element) {
return await page.evaluate(e => e.innerText, element);
}
,
/**
* Will set the element attribute value
*/
setElementAttributeValue: async function (page, elementOrSelector, attribName, attribValue) {
if (typeof elementOrSelector === 'string') {
elementOrSelector = await self.getElementBySelector(page, elementOrSelector)
}
await page.evaluate((element, attribName, attribValue) => {
element.setAttribute(attribName, attribValue)
}, elementOrSelector, attribName, attribValue);
}
,
/**
* Will return an attribute value from an element
*/
getAttributeValueFromElement: async function (page, element, attName) {
let val = await page.evaluate((element, attName) => {
return element.getAttribute(attName)
}, element, attName);
if (typeof val === 'object') {
var size = Object.keys(val).length;
if (size === 0) {
return null
}
}
return val
}
,
/**
* Will click on a an element which happens to be before or after a label.
*
* @param page the puppeteer page
* @param innerHTML set to true to look on the label's innerHTML. False for innerText
* @param labelText the label's innerHTML
* @param caseSensitive toggle this to find the exact text or to ignore higher/lower cases
* @param includes set to true if the element contains()
* @param identical set to true if the label has the EXACT text ( === )
* @param isElementAfterLabel set to false if the element is before the label (rtl pages)
*/
clickOnElementWithAdjacentLabel: async function (page,
labelText,
innerHTML = true,
caseSensitive = true,
includes = false,
identical = true,
isElementAfterLabel = false) {
let inputLabel = await self.getElementByText(page, 'label', labelText, caseSensitive, includes, identical, innerHTML);
let inputEle = undefined;
if (isElementAfterLabel) {
inputEle = await self.getNextSibling(page, inputLabel)
} else {
inputEle = await self.getPreviousSibling(page, inputLabel)
}
await self.clickOnElement(page, inputEle)
}
,
/**
* Will return the inner html of an element
*/
getInnerHTMLFromElement: async function (element) {
return await (await element.getProperty('innerHTML')).jsonValue();
}
,
/**
* Will kill the browser
*/
close: async function (browser) {
try {
await browser.close()
} catch (e) {
}
}
,
/**
* Will select an element from drop down (from <select>)
* @param page the puppetter page
* @param dropDownSelectorOrElement the <select> element, in a form of selector or element
* @param optionSelectorOrElementToSelect the <option> to select from the drop down
* @param delayAfterClick how long to wait after click
* @param selectorToFindAfterClick selector to search afterwards
* @param howLongToWaitForSelector if you chose to wait afterwards, set how long
* @param delayAfterSelectorFound if you chose to wait afterwards, set how long to wait after
*/
selectFromDropDown: async function (page,
dropDownSelectorOrElement,
optionSelectorOrElementToSelect,
delayAfterClick = 0,
selectorToFindAfterClick = null,
howLongToWaitForSelector = null,
delayAfterSelectorFound = 1500) {
// if didn't find the element, recall function
let dropDownEle = dropDownSelectorOrElement;
if (isSelector(dropDownSelectorOrElement)) {
dropDownEle = await self.getElementBySelector(page, dropDownSelectorOrElement)
}
let optionEle = optionSelectorOrElementToSelect;
if (isSelector(optionSelectorOrElementToSelect)) {
optionEle = await self.getElementBySelector(page, optionSelectorOrElementToSelect)
}
try {
await page.evaluate((optionEle, dropDownEle) => {
optionEle.selected = true;
const event = new Event('change', {bubbles: true});
dropDownEle.dispatchEvent(event);
}, optionEle, dropDownEle);
} catch (e) {
let optionSelect = await tools.promptUser("didnt find the option: " + optionSelectorOrElementToSelect + " please write the right selector here. Type 'skip' if you already selected the option yourself");
if (optionSelect !== undefined && optionSelect.toLowerCase() === 'skip') {
console.log("skipping")
} else if (optionSelect.toLowerCase() !== 'skip') {
await self.selectFromDropDown(page, dropDownSelectorOrElement, optionSelect, delayAfterClick, selectorToFindAfterClick, howLongToWaitForSelector, delayAfterSelectorFound);
return
}
}
await tools.delay(delayAfterClick);
if (selectorToFindAfterClick != null) {
await self.waitForSelector(page, selectorToFindAfterClick, howLongToWaitForSelector, delayAfterSelectorFound)
}
}
,
/**
* Will press ENTER on the keyboard
**/
pressEnter: async function (page, delayAfterPress = 0, selectorToFindAfterClick = null, howLongToWaitForSelector = null, delayAfterSelectorFound = 1500) {
await page.keyboard.press('Enter');
await tools.delay(delayAfterPress);
if (selectorToFindAfterClick != null) {
await self.waitForSelector(page, selectorToFindAfterClick, howLongToWaitForSelector, delayAfterSelectorFound)
}
}
,
/**
* Will type some text
**/
type: async function (page, text, typeDelay = 20, delayAfter = 0) {
await page.keyboard.type(text, {delay: typeDelay});
await tools.delay(delayAfter)
}
,
/**
* Will press the esc key
*/
pressEsc: async function (page, delayAfterPress = 0, selectorToFindAfterClick = null, howLongToWaitForSelector = null, delayAfterSelectorFound = 1500) {
await page.keyboard.press('Escape');
await tools.delay(delayAfterPress);
if (selectorToFindAfterClick != null) {
await self.waitForSelector(page, selectorToFindAfterClick, howLongToWaitForSelector, delayAfterSelectorFound)
}
}
,
/**
* Will check if the selector/element is visible
*/
isElementVisible: async function (page, selector, element = null) {
if (selector !== null) {
return await page.waitForSelector(selector, {
visible: true
})
}
if (element !== null) {
let displayVal = await page.evaluateHandle(el => el.getAttribute('style'), element);
let val = await displayVal.jsonValue();
return !val.includes('none')
}
}
,
/**
* Will return only visible elements from a list of elements
*/
getOnlyVisibleElements: async function (page, elesList) {
let eles = [];
for (let i = 0; i <= elesList.length; i++) {
let isEleVisible = await self.isElementVisible(page, null, elesList[i]);
if (isEleVisible) {
eles.push(elesList[i])
}
}
return eles
}
,
/**
* Will check if element has attribute
*/
isElementHasAttribute: async function (page, eleOrSelector, attName) {
if (typeof eleOrSelector === 'string') {
eleOrSelector = await self.getElementBySelector(page, eleOrSelector)
}
return await page.evaluate((element, att) => {
return element.getAttribute(att)
}, eleOrSelector, attName) !== null;
}
,
/**
* Will remove elements contains a certain text from a list
*
* @param page the puppeteer page
* @param elesList the list of elements to search the inner text
* @param text the inner text to look for
* @param caseSensitive ignore capitals etc...
* @param includes true if only included part from element text comprise your text, and not all of it
* @param identical true for exact text match
*/
removeElementsContainTextFromList: async function (page, elesList, text, caseSensitive = false, includes = true, identical = false) {
if (!caseSensitive) {
text = text.toLowerCase()
}
let fitEles = [];
for (let i = 0; i <= elesList.length; i++) {
let innerTxt = await self.getInnerHTML(page, elesList[i]);
if (innerTxt !== null && innerTxt !== "") {
innerTxt = innerTxt.toLowerCase();
if (includes && !innerTxt.includes(text)) {
fitEles.push(elesList[i])
} else if (identical && innerTxt !== text) {
fitEles.push(elesList[i])
}
} else {
fitEles.push(elesList[i])
}
}
return fitEles
}
}
;
async function mClick(page,
btnSelector = null,
btnElement = null,
delayAfterClick = 0,
selectorToFindAfterClick = null,
howLongToWaitForSelector = null,
delayAfterSelectorFound = 1500) {
if (btnElement !== null) {
await btnElement.click()
} else {
await page.click(btnSelector);
}
await tools.delay(delayAfterClick);
if (selectorToFindAfterClick != null) {
await self.waitForSelector(page, selectorToFindAfterClick, howLongToWaitForSelector, delayAfterSelectorFound)
}
}