-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathwd_helper.au3
3628 lines (3212 loc) · 162 KB
/
wd_helper.au3
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
#include-once
; standard UDF's
#include <File.au3> ; Needed For _WD_UpdateDriver
#include <InetConstants.au3>
#include <Misc.au3> ; Needed For _WD_UpdateDriver >> _VersionCompare
#include <WinAPIFiles.au3> ; Needed For _WD_UpdateDriver >> _WinAPI_GetBinaryType and _WD_DownloadFile >> _WinAPI_FileInUse
; WebDriver related UDF's
#include "wd_core.au3"
#Region Copyright
#cs
* WD_Helper.au3
*
* MIT License
*
* Copyright (c) 2023 Dan Pollak (@Danp2)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
#ce
#EndRegion Copyright
#Region Many thanks to:
#cs
- Jonathan Bennett (@Jon) and the AutoIt Team
- Thorsten Willert (@Stilgar), author of FF.au3, which I've used as a model
- Michał Lipok (@mLipok) for all his contribution
#ce
#EndRegion Many thanks to:
#Tidy_Parameters=/tcb=-1
#Region Global Constants
Global Enum _
$_WD_OPTION_None = 0, _
$_WD_OPTION_Visible = 1, _
$_WD_OPTION_Enabled = 2, _
$_WD_OPTION_Element = 4, _
$_WD_OPTION_NoMatch = 8, _
$_WD_OPTION_Hidden = 16
Global Enum _
$_WD_OPTION_Standard, _
$_WD_OPTION_Advanced
Global Enum _
$_WD_STATUS_Invalid, _
$_WD_STATUS_Valid, _
$_WD_STATUS_Reconnect
Global Enum _
$_WD_TARGET_FirstTab, _
$_WD_TARGET_LastTab
Global Enum _
$_WD_BUTTON_Left = 0, _
$_WD_BUTTON_Middle = 1, _
$_WD_BUTTON_Right = 2
Global Enum _
$_WD_STORAGE_Local = 0, _
$_WD_STORAGE_Session = 1
Global Enum _ ; _WD_FrameList() , _WD_FrameListFindElement()
$_WD_FRAMELIST_Absolute = 0, _
$_WD_FRAMELIST_Relative = 1, _
$_WD_FRAMELIST_Attributes = 2, _
$_WD_FRAMELIST_URL = 3, _
$_WD_FRAMELIST_BodyID = 4, _
$_WD_FRAMELIST_FrameVisibility = 5, _
$_WD_FRAMELIST_MatchedElements = 6, _ ; array of matched element from _WD_FrameListFindElement()
$_WD_FRAMELIST__COUNTER
#Tidy_ILC_Pos=42
Global Enum _ ; https://www.w3schools.com/jsref/prop_doc_readystate.asp
$_WD_READYSTATE_Uninitialized, _ ; Has not started loading
$_WD_READYSTATE_Loading, _ ; Is loading
$_WD_READYSTATE_Loaded, _ ; Has been loaded
$_WD_READYSTATE_Interactive, _ ; Has loaded enough to interact with
$_WD_READYSTATE_Complete, _ ; Fully loaded
$_WD_READYSTATE__COUNTER
#Tidy_ILC_Pos=0
Global Const $aWD_READYSTATE[$_WD_READYSTATE__COUNTER][2] = [ _
["uninitialized", "Has not started loading"], _
["loading", "Is loading"], _
["loaded", "Has been loaded"], _
["interactive", "Has loaded enough to interact with"], _
["complete", "Fully loaded"] _
]
Global Enum _ ; Column positions of $aWD_READYSTATE
$_WD_READYSTATE_State, _
$_WD_READYSTATE_Desc
#EndRegion Global Constants
; #FUNCTION# ====================================================================================================================
; Name ..........: _WD_NewTab
; Description ...: Create new tab in current browser session.
; Syntax ........: _WD_NewTab($sSession[, $bSwitch = Default[, $iTimeout = Default[, $sURL = Default[, $sFeatures = Default]]]])
; Parameters ....: $sSession - Session ID from _WD_CreateSession
; $bSwitch - [optional] Switch session context to new tab? Default is True
; $iTimeout - [optional] Period of time (in milliseconds) to wait before exiting function
; $sURL - [optional] URL to be loaded in new tab
; $sFeatures - [optional] Comma-separated list of requested features of the new tab
; Return values .: Success - String representing handle of new tab.
; Failure - "" (empty string) and sets @error to one of the following values:
; - $_WD_ERROR_Exception
; - $_WD_ERROR_Timeout
; Author ........: Danp2
; Modified ......: mLipok
; Remarks .......: Specifying any features other than noopener or noreferrer, also has the effect of requesting a popup.
; See the link below for further details and a list of available features.
; Related .......: _WD_Window, _WD_LastHTTPResult
; Link ..........: https://developer.mozilla.org/en-US/docs/Web/API/Window/open#window_features
; Example .......: No
; ===============================================================================================================================
Func _WD_NewTab($sSession, $bSwitch = Default, $iTimeout = Default, $sURL = Default, $sFeatures = Default)
Local Const $sFuncName = "_WD_NewTab"
Local Const $sParameters = 'Parameters: Switch=' & $bSwitch & ' Timeout=' & $iTimeout & ' URL=' & $sURL & ' Features=' & $sFeatures
Local $sTabHandle = '', $sLastTabHandle, $hWaitTimer, $iTabIndex, $aTemp
If $bSwitch = Default Then $bSwitch = True
If $iTimeout = Default Then $iTimeout = $_WD_DefaultTimeout
If $sURL = Default Then $sURL = ''
If $sFeatures = Default Then $sFeatures = ''
; Get handle for current tab
Local $sCurrentTabHandle = _WD_Window($sSession, 'window')
If $sFeatures = '' Then
$sTabHandle = _WD_Window($sSession, 'new', '{"type":"tab"}')
If @error = $_WD_ERROR_Success Then
_WD_Window($sSession, 'Switch', '{"handle":"' & $sTabHandle & '"}')
If $sURL Then _WD_Navigate($sSession, $sURL)
If Not $bSwitch Then _WD_Window($sSession, 'Switch', '{"handle":"' & $sCurrentTabHandle & '"}')
Else
Return SetError(__WD_Error($sFuncName, $_WD_ERROR_Exception, $sParameters), 0, $sTabHandle)
EndIf
Else
Local $aHandles = _WD_Window($sSession, 'handles')
If @error <> $_WD_ERROR_Success Or Not IsArray($aHandles) Then
Return SetError(__WD_Error($sFuncName, $_WD_ERROR_Exception, $sParameters), 0, $sTabHandle)
EndIf
Local $iTabCount = UBound($aHandles)
; Get handle to current last tab
$sLastTabHandle = $aHandles[$iTabCount - 1]
If $sCurrentTabHandle Then
; Search for current tab handle in array of tab handles. If not found,
; then make the current tab handle equal to the last tab
$iTabIndex = _ArraySearch($aHandles, $sCurrentTabHandle)
If @error Then
$sCurrentTabHandle = $sLastTabHandle
$iTabIndex = $iTabCount - 1
EndIf
Else
_WD_Window($sSession, 'Switch', '{"handle":"' & $sLastTabHandle & '"}')
$sCurrentTabHandle = $sLastTabHandle
$iTabIndex = $iTabCount - 1
EndIf
_WD_ExecuteScript($sSession, "window.open(arguments[0], '', arguments[1])", '"' & $sURL & '","' & $sFeatures & '"')
If @error <> $_WD_ERROR_Success Then
Return SetError(__WD_Error($sFuncName, $_WD_ERROR_Exception, $sParameters), 0, $sTabHandle)
EndIf
$hWaitTimer = TimerInit()
While 1
$aTemp = _WD_Window($sSession, 'handles')
If UBound($aTemp) > $iTabCount Then
$sTabHandle = $aTemp[$iTabIndex + 1]
ExitLoop
EndIf
If TimerDiff($hWaitTimer) > $iTimeout Then Return SetError(__WD_Error($sFuncName, $_WD_ERROR_Timeout, $sParameters), 0, $sTabHandle)
__WD_Sleep(10)
If @error Then Return SetError(__WD_Error($sFuncName, @error, $sParameters), 0, $sTabHandle)
WEnd
If $bSwitch Then
_WD_Window($sSession, 'Switch', '{"handle":"' & $sTabHandle & '"}')
Else
_WD_Window($sSession, 'Switch', '{"handle":"' & $sCurrentTabHandle & '"}')
EndIf
EndIf
Return SetError(__WD_Error($sFuncName, $_WD_ERROR_Success, $sParameters), 0, $sTabHandle)
EndFunc ;==>_WD_NewTab
; #FUNCTION# ====================================================================================================================
; Name ..........: _WD_Attach
; Description ...: Attach to existing browser tab.
; Syntax ........: _WD_Attach($sSession, $sSearch[, $sMode = Default])
; Parameters ....: $sSession - Session ID from _WD_CreateSession
; $sSearch - String to search for
; $sMode - [optional] One of the following search modes:
; |Title (default)
; |URL
; |HTML
; Return values .: Success - String representing handle of matching tab.
; Failure - "" (empty string) and sets @error to one of the following values:
; - $_WD_ERROR_InvalidDataType
; - $_WD_ERROR_NoMatch
; - $_WD_ERROR_GeneralError
; Author ........: Danp2
; Modified ......:
; Remarks .......:
; Related .......: _WD_Window, _WD_LastHTTPResult
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _WD_Attach($sSession, $sSearch, $sMode = Default)
Local Const $sFuncName = "_WD_Attach"
Local Const $sParameters = 'Parameters: Search=' & $sSearch & ' Mode=' & $sMode
Local $sTabHandle = '', $bFound = False, $sCurrentTab = '', $aHandles
Local $iErr = $_WD_ERROR_Success
If $sMode = Default Then $sMode = 'title'
$aHandles = _WD_Window($sSession, 'handles')
$iErr = @error
If $iErr = $_WD_ERROR_Success Then
$sCurrentTab = _WD_Window($sSession, 'window')
For $sHandle In $aHandles
_WD_Window($sSession, 'Switch', '{"handle":"' & $sHandle & '"}')
Switch $sMode
Case "title", "url"
If StringInStr(_WD_Action($sSession, $sMode), $sSearch) > 0 Then
$bFound = True
$sTabHandle = $sHandle
ExitLoop
EndIf
Case 'html'
If StringInStr(_WD_GetSource($sSession), $sSearch) > 0 Then
$bFound = True
$sTabHandle = $sHandle
ExitLoop
EndIf
Case Else
Return SetError(__WD_Error($sFuncName, $_WD_ERROR_InvalidDataType, "(Title|URL|HTML) $sMode=>" & $sMode), 0, $sTabHandle)
EndSwitch
Next
If Not $bFound Then
; Restore prior active tab
If $sCurrentTab <> '' Then
_WD_Window($sSession, 'Switch', '{"handle":"' & $sCurrentTab & '"}')
EndIf
$iErr = $_WD_ERROR_NoMatch
EndIf
ElseIf Not $_WD_DetailedErrors Then
$iErr = $_WD_ERROR_GeneralError
EndIf
Return SetError(__WD_Error($sFuncName, $iErr, $sParameters), 0, $sTabHandle)
EndFunc ;==>_WD_Attach
; #FUNCTION# ====================================================================================================================
; Name ..........: _WD_LinkClickByText
; Description ...: Simulate a mouse click on a link with text matching the provided string.
; Syntax ........: _WD_LinkClickByText($sSession, $sText[, $bPartial = Default[, $sStartNodeID = Default]])
; Parameters ....: $sSession - Session ID from _WD_CreateSession
; $sText - Text to find in link
; $bPartial - [optional] Search by partial text? Default is True
; $sStartNodeID - [optional] Element ID to use as starting HTML node. Default is ""
; Return values .: Success - None.
; Failure - "" (empty string) and sets @error to one of the following values:
; - $_WD_ERROR_Exception
; - $_WD_ERROR_NoMatch
; Author ........: Danp2
; Modified ......:
; Remarks .......:
; Related .......: _WD_FindElement, _WD_ElementAction, _WD_LastHTTPResult
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _WD_LinkClickByText($sSession, $sText, $bPartial = Default, $sStartNodeID = Default)
Local Const $sFuncName = "_WD_LinkClickByText"
Local Const $sParameters = 'Parameters: Text=' & $sText & ' Partial=' & $bPartial & ' StartElement=' & $sStartNodeID
If $bPartial = Default Then $bPartial = True
If $sStartNodeID = Default Then $sStartNodeID = ""
Local $sElement = _WD_FindElement($sSession, ($bPartial) ? $_WD_LOCATOR_ByPartialLinkText : $_WD_LOCATOR_ByLinkText, $sText, $sStartNodeID)
Local $iErr = @error
If $iErr = $_WD_ERROR_Success Then
_WD_ElementAction($sSession, $sElement, 'click')
$iErr = @error
If $iErr <> $_WD_ERROR_Success And Not $_WD_DetailedErrors Then
$iErr = $_WD_ERROR_Exception
EndIf
Else
$iErr = $_WD_ERROR_NoMatch
EndIf
Return SetError(__WD_Error($sFuncName, $iErr, $sParameters), 0, "")
EndFunc ;==>_WD_LinkClickByText
; #FUNCTION# ====================================================================================================================
; Name ..........: _WD_WaitElement
; Description ...: Wait for an element in the current tab before returning.
; Syntax ........: _WD_WaitElement($sSession, $sStrategy, $sSelector[, $iDelay = Default[, $iTimeout = Default[, $iOptions = Default]]])
; Parameters ....: $sSession - Session ID from _WD_CreateSession
; $sStrategy - Locator strategy. See defined constant $_WD_LOCATOR_* for allowed values
; $sSelector - Indicates how the WebDriver should traverse through the HTML DOM to locate the desired element(s).
; $iDelay - [optional] Milliseconds to wait before initially checking status
; $iTimeout - [optional] Period of time (in milliseconds) to wait before exiting function
; $iOptions - [optional] Binary flags to perform additional actions:
; |$_WD_OPTION_None (0) = No optional feature processing
; |$_WD_OPTION_Visible (1) = Confirm element is visible
; |$_WD_OPTION_Enabled (2) = Confirm element is enabled
; |$_WD_OPTION_NoMatch (8) = Confirm element is not found
; |$_WD_OPTION_Hidden (16) = Confirm element is not visible
; Return values .: Success - Element ID returned by web driver.
; Failure - "" (empty string) and sets @error to one of the following values:
; - $_WD_ERROR_Exception
; - $_WD_ERROR_InvalidArgue
; - $_WD_ERROR_Timeout
; - $_WD_ERROR_UserAbort
; Author ........: Danp2
; Modified ......: mLipok
; Remarks .......:
; Related .......: _WD_FindElement, _WD_ElementAction, _WD_LastHTTPResult
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _WD_WaitElement($sSession, $sStrategy, $sSelector, $iDelay = Default, $iTimeout = Default, $iOptions = Default)
Local Const $sFuncName = "_WD_WaitElement"
Local Const $sParameters = 'Parameters: Strategy=' & $sStrategy & ' Selector=' & $sSelector & ' Delay=' & $iDelay & ' Timeout=' & $iTimeout & ' Options=' & $iOptions
Local $iErr, $sElement, $bIsVisible = True, $bIsEnabled = True
$_WD_HTTPRESULT = 0
$_WD_HTTPRESPONSE = ''
If $iDelay = Default Then $iDelay = 0
If $iTimeout = Default Then $iTimeout = $_WD_DefaultTimeout
If $iOptions = Default Then $iOptions = $_WD_OPTION_None
Local Const $bVisible = BitAND($iOptions, $_WD_OPTION_Visible)
Local Const $bEnabled = BitAND($iOptions, $_WD_OPTION_Enabled)
Local Const $bNoMatch = BitAND($iOptions, $_WD_OPTION_NoMatch)
Local Const $bHidden = BitAND($iOptions, $_WD_OPTION_Hidden)
; Other options aren't valid if No Match or Hidden option is supplied
If ($bNoMatch And $iOptions <> $_WD_OPTION_NoMatch) Or _
($bHidden And $iOptions <> $_WD_OPTION_Hidden) Then
$iErr = $_WD_ERROR_InvalidArgue
Else
__WD_Sleep($iDelay)
$iErr = @error
; prevent multiple errors https://github.com/Danp2/au3WebDriver/pull/290#issuecomment-1100707095
Local $_WD_DEBUG_Saved = $_WD_DEBUG ; save current DEBUG level
; Prevent logging from _WD_FindElement if not in Full debug mode
If $_WD_DEBUG <> $_WD_DEBUG_Full Then $_WD_DEBUG = $_WD_DEBUG_None
Local $hWaitTimer = TimerInit()
While 1
If $iErr Then ExitLoop
$sElement = _WD_FindElement($sSession, $sStrategy, $sSelector)
$iErr = @error
If $iErr <> $_WD_ERROR_Success And $iErr <> $_WD_ERROR_NoMatch Then
; Exit loop if unexpected error occurs
ExitLoop
ElseIf $iErr = $_WD_ERROR_NoMatch And $bNoMatch Then
; if element wasn't found and "no match" option is active
; exit loop indicating success
$iErr = $_WD_ERROR_Success
ExitLoop
ElseIf $iErr = $_WD_ERROR_Success And Not $bNoMatch Then
; if element was found and "no match" option isn't active
; check other options
If $bVisible Or $bHidden Then
$bIsVisible = _WD_ElementAction($sSession, $sElement, 'displayed')
If @error Then
$bIsVisible = False
EndIf
EndIf
If $bEnabled Then
$bIsEnabled = _WD_ElementAction($sSession, $sElement, 'enabled')
If @error Then
$bIsEnabled = False
EndIf
EndIf
Select
Case $bHidden
If Not $bIsVisible Then ExitLoop
Case $bIsVisible And $bIsEnabled
ExitLoop
Case Else
$sElement = ''
EndSelect
EndIf
If (TimerDiff($hWaitTimer) > $iTimeout) Then
$iErr = $_WD_ERROR_Timeout
ExitLoop
EndIf
__WD_Sleep(10)
$iErr = @error
WEnd
$_WD_DEBUG = $_WD_DEBUG_Saved ; restore DEBUG level
EndIf
Return SetError(__WD_Error($sFuncName, $iErr, $sParameters), 0, $sElement)
EndFunc ;==>_WD_WaitElement
; #FUNCTION# ====================================================================================================================
; Name ..........: _WD_WaitScript
; Description ...: Wait for a JavaScript snippet to return true.
; Syntax ........: _WD_WaitScript($sSession, $sJavaScript[, $iDelay = Default[, $iTimeout = Default[, $iOptions = Default]]])
; Parameters ....: $sSession - Session ID from _WD_CreateSession
; $sJavaScript - JavaScript to run
; $iDelay - [optional] Milliseconds to wait before initially checking status
; $iTimeout - [optional] Period of time (in milliseconds) to wait before exiting function
; Return values .: Success - True
; Failure - False and sets @error to one of the following values:
; - $_WD_ERROR_InvalidArgue
; - $_WD_ERROR_RetValue
; - $_WD_ERROR_Exception
; - $_WD_ERROR_Timeout
; - $_WD_ERROR_UserAbort
; Author ........: yehiaserag
; Modified ......:
; Remarks .......: The Javascript needs to return either True or False.
; Related .......: _WD_ExecuteScript
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _WD_WaitScript($sSession, $sJavaScript, $iDelay = Default, $iTimeout = Default)
Local Const $sFuncName = "_WD_WaitScript"
Local Const $sParameters = 'Parameters: JavaScript=' & $sJavaScript & ' Delay=' & $iDelay & ' Timeout=' & $iTimeout
Local $iErr
Local $bValue = False
If $iDelay = Default Then $iDelay = 0
If $iTimeout = Default Then $iTimeout = $_WD_DefaultTimeout
If StringLeft($sJavaScript, 6) <> "return" Then
$iErr = $_WD_ERROR_InvalidArgue
Else
__WD_Sleep($iDelay)
$iErr = @error
; prevent multiple errors https://github.com/Danp2/au3WebDriver/pull/290#issuecomment-1100707095
Local $_WD_DEBUG_Saved = $_WD_DEBUG ; save current DEBUG level
; Prevent logging from _WD_ExecuteScript if not in Full debug mode
If $_WD_DEBUG <> $_WD_DEBUG_Full Then $_WD_DEBUG = $_WD_DEBUG_None
Local $hWaitTimer = TimerInit()
While 1
If $iErr Then ExitLoop
$bValue = _WD_ExecuteScript($sSession, 'return !!((function(){' & $sJavaScript & '})())', Default, Default, $_WD_JSON_Value)
$iErr = @error
If $iErr <> $_WD_ERROR_Success Then
; Exit loop if unexpected error occurs
ExitLoop
ElseIf $bValue = False Then
If (TimerDiff($hWaitTimer) > $iTimeout) Then
$iErr = $_WD_ERROR_Timeout
ExitLoop
EndIf
__WD_Sleep(10)
$iErr = @error
Else
$iErr = $_WD_ERROR_Success
ExitLoop
EndIf
WEnd
$_WD_DEBUG = $_WD_DEBUG_Saved ; restore DEBUG level
EndIf
Return SetError(__WD_Error($sFuncName, $iErr, $sParameters), 0, $bValue)
EndFunc ;==>_WD_WaitScript
; #FUNCTION# ====================================================================================================================
; Name ..........: _WD_DebugSwitch
; Description ...: Switch to new debug level or switch back to saved debug level
; Syntax ........: _WD_DebugSwitch([$vMode = Default])
; Parameters ....: $vMode - [optional] Set new $_WD_DEBUG level. When not specified (Default) restore saved debug level.
; Return values .: Success - current stack size
; Failure - negative values indicate an error
; Author ........: mLipok
; Modified ......:
; Remarks .......: @error and @extended values are preserved by this function and did not originate within it
; Related .......:
; Link ..........:
; Example .......: _WD_DebugSwitch($_WD_DEBUG_Full)
; ===============================================================================================================================
Func _WD_DebugSwitch($vMode = Default, $iErr = @error, $iExt = @extended)
Local Const $sFuncName = "_WD_DebugSwitch"
Local Static $a_WD_DEBUG_SavedStack[0] ; first usage - empty stack array
Local $iStackSize = UBound($a_WD_DEBUG_SavedStack)
Local $sMessage = ''
If $vMode = Default Then ; restoring saved debug level
If $iStackSize Then
$_WD_DEBUG = $a_WD_DEBUG_SavedStack[$iStackSize - 1] ; restore previous debug level from last element on the stack
$iStackSize -= 1 ; decrease stack size
ReDim $a_WD_DEBUG_SavedStack[$iStackSize] ; trim array - stack last element
Else
$iStackSize = -1
$sMessage = 'There are no saved debug levels'
EndIf
ElseIf IsInt($vMode) And $vMode >= $_WD_DEBUG_None And $vMode <= $_WD_DEBUG_Full Then ; setting new debug level
$iStackSize += 1 ; increase stack size
ReDim $a_WD_DEBUG_SavedStack[$iStackSize] ; resize array - add new position to the stack
$a_WD_DEBUG_SavedStack[$iStackSize - 1] = $_WD_DEBUG ; store current debug level to the stack
$_WD_DEBUG = $vMode ; set new debug level
Else
$iStackSize = -2
$sMessage = 'Invalid argument in function-call'
EndIf
$sMessage &= " / " & (($iStackSize < 0) ? (" error code: ") : (" stack size: ")) & $iStackSize
__WD_ConsoleWrite($sFuncName & ": " & $sMessage, $_WD_DEBUG_Info)
Return SetError($iErr, $iExt, $iStackSize) ; do not use __WD_Error() here as $iErr and $iExt are preserved and not belongs to this function
EndFunc ;==>_WD_DebugSwitch
; #FUNCTION# ====================================================================================================================
; Name ..........: _WD_GetMouseElement
; Description ...: Retrieves reference to element below mouse pointer.
; Syntax ........: _WD_GetMouseElement($sSession)
; Parameters ....: $sSession - Session ID from _WD_CreateSession
; Return values .: Success - Element ID returned by web driver.
; Failure - Response from web driver and sets @error returned from _WD_ExecuteScript()
; Author ........: Danp2
; Modified ......: mLipok
; Remarks .......:
; Related .......: _WD_ExecuteScript, _WD_LastHTTPResult
; Link ..........: https://stackoverflow.com/questions/24538450/get-element-currently-under-mouse-without-using-mouse-events
; Example .......: No
; ===============================================================================================================================
Func _WD_GetMouseElement($sSession)
Local Const $sFuncName = "_WD_GetMouseElement"
Local $sScript = "return Array.from(document.querySelectorAll(':hover')).pop()"
Local $sElement = _WD_ExecuteScript($sSession, $sScript, '', Default, $_WD_JSON_Element)
Local $iErr = @error
Return SetError(__WD_Error($sFuncName, $iErr, $sElement), 0, $sElement)
EndFunc ;==>_WD_GetMouseElement
; #FUNCTION# ====================================================================================================================
; Name ..........: _WD_GetElementFromPoint
; Description ...: Retrieves reference to element at specified point.
; Syntax ........: _WD_GetElementFromPoint($sSession, $iX, $iY)
; Parameters ....: $sSession - Session ID from _WD_CreateSession
; $iX - an integer value
; $iY - an integer value
; Return values .: Success - Element ID returned by web driver.
; Failure - "" (empty string) and @error is set to one of the following values:
; - $_WD_ERROR_RetValue
; - $_WD_ERROR_InvalidArgue
; Author ........: Danp2
; Modified ......: mLipok
; Remarks .......: @extended is set to 1 if the browsing context changed during the function call
; Related .......: _WD_ExecuteScript, _WD_LastHTTPResult
; Link ..........: https://stackoverflow.com/questions/31910534/executing-javascript-elementfrompoint-through-selenium-driver/32574543#32574543
; Example .......: No
; ===============================================================================================================================
Func _WD_GetElementFromPoint($sSession, $iX, $iY)
Local Const $sFuncName = "_WD_GetElementFromPoint"
Local Const $sParameters = 'Parameters: X=' & $iX & ' Y=' & $iY
Local $sResponse, $oJSON, $sElement = ""
Local $sTagName, $sParams, $aCoords, $iFrame = 0, $oERect
Local $sScript1 = "return document.elementFromPoint(arguments[0], arguments[1]);"
Local $sScript2 = "return new Array(window.pageXOffset, window.pageYOffset);"
Local $iErr = $_WD_ERROR_Success, $sResult, $bIsNull
; https://developer.mozilla.org/en-US/docs/Web/API/Document/elementFromPoint
; If the specified point is outside the visible bounds of the document or either
; coordinate is negative, the result is null
If $iX < 0 Or $iY < 0 Then
$iErr = $_WD_ERROR_InvalidArgue
EndIf
While $iErr = $_WD_ERROR_Success
$sParams = $iX & ", " & $iY
$sResponse = _WD_ExecuteScript($sSession, $sScript1, $sParams)
If @error Then
$iErr = $_WD_ERROR_RetValue
ExitLoop
EndIf
$oJSON = Json_Decode($sResponse)
$sElement = Json_Get($oJSON, $_WD_JSON_Element)
If @error Then
$sResult = Json_Get($oJSON, $_WD_JSON_Value)
$bIsNull = (IsKeyword($sResult) = $KEYWORD_NULL)
If Not $bIsNull Then
$iErr = $_WD_ERROR_RetValue
EndIf
ExitLoop
Else
$sTagName = _WD_ElementAction($sSession, $sElement, "Name")
If Not StringInStr($sTagName, "frame") Then ; check <iframe> and <frame> element
ExitLoop
EndIf
$aCoords = _WD_ExecuteScript($sSession, $sScript2, $_WD_EmptyDict, Default, $_WD_JSON_Value)
If @error Then
$iErr = $_WD_ERROR_RetValue
ExitLoop
EndIf
$oERect = _WD_ElementAction($sSession, $sElement, 'rect')
; changing the coordinates in relation to left top corner of frame
$iX -= ($oERect.Item('x') - Int($aCoords[0]))
$iY -= ($oERect.Item('y') - Int($aCoords[1]))
_WD_FrameEnter($sSession, $sElement)
$iFrame = 1
EndIf
WEnd
Return SetError(__WD_Error($sFuncName, $iErr, $sParameters, $iFrame), $iFrame, $sElement)
EndFunc ;==>_WD_GetElementFromPoint
; #FUNCTION# ====================================================================================================================
; Name ..........: _WD_GetFrameCount
; Description ...: Returns the number of frames/iframes in the current document context.
; Syntax ........: _WD_GetFrameCount($sSession)
; Parameters ....: $sSession - Session ID from _WD_CreateSession
; Return values .: Success - Number of frames
; Failure - 0 and sets @error to $_WD_ERROR_Exception
; Author ........: Decibel, Danp2
; Modified ......: mLipok
; Remarks .......: Nested frames are not included in the frame count
; Related .......: _WD_ExecuteScript, _WD_LastHTTPResult
; Link ..........: https://www.w3schools.com/jsref/prop_win_length.asp
; Example .......: No
; ===============================================================================================================================
Func _WD_GetFrameCount($sSession)
Local Const $sFuncName = "_WD_GetFrameCount"
Local $iValue = _WD_ExecuteScript($sSession, "return window.frames.length", Default, Default, $_WD_JSON_Value)
Local $iErr = @error
If $iErr Then $iValue = 0
Return SetError(__WD_Error($sFuncName, $iErr), 0, Number($iValue))
EndFunc ;==>_WD_GetFrameCount
; #FUNCTION# ====================================================================================================================
; Name ..........: _WD_IsWindowTop
; Description ...: Returns a boolean of the session being at the top level, or in a frame(s).
; Syntax ........: _WD_IsWindowTop($sSession)
; Parameters ....: $sSession - Session ID from _WD_CreateSession
; Return values .: Success - Boolean response.
; Failure - Response from webdriver and sets @error returned from _WD_ExecuteScript()
; Author ........: Decibel
; Modified ......: mLipok
; Remarks .......:
; Related .......: _WD_ExecuteScript, _WD_LastHTTPResult
; Link ..........: https://www.w3schools.com/jsref/prop_win_top.asp
; Example .......: No
; ===============================================================================================================================
Func _WD_IsWindowTop($sSession)
Local Const $sFuncName = "_WD_IsWindowTop"
Local $blnResult = _WD_ExecuteScript($sSession, "return window.top == window.self", Default, Default, $_WD_JSON_Value)
Local $iErr = @error
Return SetError(__WD_Error($sFuncName, $iErr), 0, $blnResult)
EndFunc ;==>_WD_IsWindowTop
; #FUNCTION# ====================================================================================================================
; Name ..........: _WD_FrameEnter
; Description ...: Enter the specified frame.
; Syntax ........: _WD_FrameEnter($sSession, $vIdentifier)
; Parameters ....: $sSession - Session ID from _WD_CreateSession
; $vIdentifier - Target frame identifier. Can be any of the following:
; |Null - Return to top-most browsing context
; |String - Element ID from _WD_FindElement or path like 'null/2/0'
; |Integer - 0-based index of frames
; Return values .: Success - True.
; Failure - WD Response error message (E.g. "no such frame") and sets @error to one of the following values:
; - $_WD_ERROR_Exception
; Author ........: Decibel
; Modified ......: Danp2, mLipok, jchd
; Remarks .......: You can drill-down into nested frames by calling this function repeatedly or use identifier like 'null/2/0'
; Related .......: _WD_Window, _WD_LastHTTPResult
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _WD_FrameEnter($sSession, $vIdentifier)
Local Const $sFuncName = "_WD_FrameEnter"
If String($vIdentifier) = 'null' Then $vIdentifier = Null ; String must be used because checking 0 = 'null' is True
Local Const $bIsIdentifierNull = (IsKeyword($vIdentifier) = $KEYWORD_NULL)
Local Const $sParameters = 'Parameters: Identifier=' & ($bIsIdentifierNull ? ("Null") : ($vIdentifier))
Local $sValue, $sMessage = '', $sOption, $sResponse, $oJSON
Local $iErr = $_WD_ERROR_Success
; must start with null or digit, must have at least one slash (may have many slashes but should not be followed one per other), must end with digit
Local Const $bIdentifierAsPath = StringRegExp($vIdentifier, "(?i)\A(?:Null|\d+)(?:\/\d+)+\Z", $STR_REGEXPMATCH)
If $bIdentifierAsPath Then
; will be processed below
ElseIf $bIsIdentifierNull Then
$sOption = '{"id":null}'
ElseIf IsInt($vIdentifier) Then
$sOption = '{"id":' & $vIdentifier & '}'
Else
$sOption = '{"id":' & __WD_JsonElement($vIdentifier) & '}'
EndIf
If Not $bIdentifierAsPath Then
$sResponse = _WD_Window($sSession, "frame", $sOption)
$iErr = @error
Else
Local $aIdentifiers = StringSplit($vIdentifier, '/')
For $i = 1 To $aIdentifiers[0]
If String($aIdentifiers[$i]) = 'null' Then
$aIdentifiers[$i] = '{"id":null}'
Else
$aIdentifiers[$i] = '{"id":' & $aIdentifiers[$i] & '}'
EndIf
$sResponse = _WD_Window($sSession, "frame", $aIdentifiers[$i])
If Not @error Then ContinueLoop
$iErr = @error
$sMessage = ' Error on ID#' & $i & ' > ' & $aIdentifiers[$i]
ExitLoop
Next
EndIf
If $iErr = $_WD_ERROR_Success Then
$oJSON = Json_Decode($sResponse)
$sValue = Json_Get($oJSON, $_WD_JSON_Value)
;*** Evaluate the response
If $sValue <> Null Then
$sValue = Json_Get($oJSON, $_WD_JSON_Error)
Else
$sValue = True
EndIf
ElseIf Not $_WD_DetailedErrors Then
$iErr = $_WD_ERROR_Exception
EndIf
Return SetError(__WD_Error($sFuncName, $iErr, $sParameters & $sMessage), 0, $sValue)
EndFunc ;==>_WD_FrameEnter
; #FUNCTION# ====================================================================================================================
; Name ..........: _WD_FrameLeave
; Description ...: Leave the current frame, to its parent.
; Syntax ........: _WD_FrameLeave($sSession)
; Parameters ....: $sSession - Session ID from _WD_CreateSession
; Return values .: Success - True.
; Failure - WD Response error message (E.g. "chrome not reachable") and sets @error to one of the following values:
; - $_WD_ERROR_Exception
; Author ........: Decibel
; Modified ......: Danp2
; Remarks .......:
; Related .......: _WD_Window, _WD_LastHTTPResult
; Link ..........: https://www.w3.org/TR/webdriver/#switch-to-parent-frame
; Example .......: No
; ===============================================================================================================================
Func _WD_FrameLeave($sSession)
Local Const $sFuncName = "_WD_FrameLeave"
Local $sValue, $oJSON, $sOption = '{}'
Local $sResponse = _WD_Window($sSession, "parent", $sOption)
Local $iErr = @error
If $iErr = $_WD_ERROR_Success Then
$oJSON = Json_Decode($sResponse)
$sValue = Json_Get($oJSON, $_WD_JSON_Value)
;*** Evaluate the response
If $sValue <> Null Then
$sValue = Json_Get($oJSON, $_WD_JSON_Error)
Else
$sValue = True
EndIf
ElseIf Not $_WD_DetailedErrors Then
$iErr = $_WD_ERROR_Exception
EndIf
Return SetError(__WD_Error($sFuncName, $iErr), 0, $sValue)
EndFunc ;==>_WD_FrameLeave
; #FUNCTION# ====================================================================================================================
; Name ..........: _WD_FrameList
; Description ...: Retrieves a detailed list of the main document and all associated frames
; Syntax ........: _WD_FrameList($sSession[, $bReturnAsArray = True[, $iDelay = 1000[, $iTimeout = Default]]])
; Parameters ....: $sSession - Session ID from _WD_CreateSession
; $bReturnAsArray - [optional] Return result as array? Default is True.
; $iDelay - [optional] Single delay before checking first frame. Default is 1000 ms
; $iTimeout - [optional] Timeout for _WD_LoadWait() calls for each frame. Default is $_WD_DefaultTimeout
; Return values .: Success - 2D array (with 7 cols) or string ( delimited with | and @CRLF ) @extended contains information about frame count
; Failure - "" (empty string) and sets @error to one of the following values:
; - $_WD_ERROR_GeneralError
; - $_WD_ERROR_Timeout
; - $_WD_ERROR_Exception
; - $_WD_ERROR_NotFound
; - $_WD_ERROR_RetValue
; - $_WD_ERROR_UserAbort
; Author ........: mLipok
; Modified ......: Danp2
; Remarks .......: The returned list of frames can depend on many factors, including geolocation, as well as problems with the local Internet
; Related .......: _WD_GetFrameCount, _WD_FrameEnter, _WD_FrameLeave
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _WD_FrameList($sSession, $bReturnAsArray = True, $iDelay = 1000, $iTimeout = Default)
Local Const $sFuncName = "_WD_FrameList"
Local Const $sParameters = 'Parameters: ReturnAsArray=' & $bReturnAsArray & ' iDelay=' & $iDelay & ' iTimeout=' & $iTimeout
Local $a_Result[0][$_WD_FRAMELIST__COUNTER], $sStartLocation = '', $sMessage = ''
Local $vResult = '', $iErr = $_WD_ERROR_Success, $iFrameCount = 0
Local Const $sElement_CallingFrameBody = _WD_ExecuteScript($sSession, "return window.document.body;", Default, Default, $_WD_JSON_Element)
If Not @error Then
__WD_Sleep($iDelay)
EndIf
If Not @error Then
$vResult = __WD_FrameList_Internal($sSession, 'null', '', False, $iTimeout)
EndIf
$iErr = @error
#Region - post processing
If $iErr = $_WD_ERROR_Success Then
; Strip last @CRLF
$vResult = StringTrimRight($vResult, 2)
; create array of frames from string returned from __WD_FrameList_Internal
_ArrayAdd($a_Result, $vResult)
; check the results
For $i = 0 To UBound($a_Result) - 1
; find "calling frame" location - set $sStartLocation
If $a_Result[$i][$_WD_FRAMELIST_BodyID] = $sElement_CallingFrameBody Then $sStartLocation = $a_Result[$i][$_WD_FRAMELIST_Absolute]
; recalculate locations from absolute path on COL0 to relative path on COL1
$a_Result[$i][$_WD_FRAMELIST_Relative] = StringRegExpReplace($a_Result[$i][$_WD_FRAMELIST_Absolute], '\A' & $sStartLocation & '\/?', '')
Next
ElseIf $iErr <> $_WD_ERROR_Timeout And $iErr <> $_WD_ERROR_UserAbort And Not $_WD_DetailedErrors Then
$iErr = $_WD_ERROR_GeneralError
EndIf
$iFrameCount = UBound($a_Result, $UBOUND_ROWS)
If $iFrameCount < 1 Then $sMessage &= 'List of frames is empty. '
; select desired DataType for the $vResult - usually string is option for testing and asking support, thus Array is returned by default
If $bReturnAsArray Then
$vResult = $a_Result
Else
$vResult = _ArrayToString($a_Result) ; getting string with recalculated locations (relative path)
If @error Then
$iErr = $_WD_ERROR_RetValue
$sMessage = 'ArrayToString conversion failed. '
$vResult = ''
EndIf
EndIf
If $sStartLocation Then ; Back to "calling frame"
_WD_FrameEnter($sSession, $sStartLocation)
$iErr = @error
If $iErr Then
$sMessage &= 'Was not able back to "calling frame".'
If Not $_WD_DetailedErrors Then $iErr = $_WD_ERROR_Exception
EndIf
Else
$sMessage &= 'Was not able to check "calling frame".'
$iErr = $_WD_ERROR_NotFound
EndIf
#EndRegion - post processing
$sMessage = ($sMessage And $_WD_DEBUG > $_WD_DEBUG_Error) ? (' Information: ' & $sMessage) : ("")
Return SetError(__WD_Error($sFuncName, $iErr, $sParameters & $sMessage, $iFrameCount), $iFrameCount, $vResult)
EndFunc ;==>_WD_FrameList
; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __WD_FrameList_Internal
; Description ...: function that is used internally in _WD_FrameList, even recursively when nested frames are available
; Syntax ........: __WD_FrameList_Internal($sSession, $sLevel, $sFrameAttributes, $bIsHidden, $iTimeout)
; Parameters ....: $sSession - Session ID from _WD_CreateSession
; $sLevel - frame location level path
; $sFrameAttributes - frame attributes in HTML format
; $bIsHidden - information about visibility of frame - taken by WebDriver
; $iTimeout - Timeout for _WD_LoadWait() calls for each frame
; Return values .: Success - string
; Failure - "" (empty string) and sets @error returned from related functions
; Author ........: mLipok
; Modified ......: Danp2
; Remarks .......:
; Related .......: _WD_FrameEnter, _WD_LoadWait, _WD_ExecuteScript, _WD_GetFrameCount, _WD_ElementAction, _WD_FrameLeave
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __WD_FrameList_Internal($sSession, $sLevel, $sFrameAttributes, $bIsHidden, $iTimeout)
Local Const $sFuncName = "__WD_FrameList_Internal"
Local Const $sParameters = 'Parameters: Level=' & $sLevel & ' IsHidden=' & $bIsHidden & ' Timeout=' & $iTimeout ; intentionally $sFrameAttributes is not listed here to not put too many data into the log
Local $iErr = $_WD_ERROR_Success, $sMessage = '', $vResult = ''
Local $s_URL = '', $sCurrentBody_ElementID = ''
#Region ; this region is prevented from redundant logging if not in Full debug mode - https://github.com/Danp2/au3WebDriver/pull/362#issuecomment-1220962556
Local Static $_WD_DEBUG_Saved = Null ; this is static because this function will be run recurrently and we need to keep outer debug level
If $_WD_DEBUG_Saved = Null Then
$_WD_DEBUG_Saved = $_WD_DEBUG ; save current DEBUG level
If $_WD_DEBUG_Saved <> $_WD_DEBUG_Full Then $_WD_DEBUG = $_WD_DEBUG_None ; Prevent logging multiple errors from __WD_FrameList_Internal
EndIf
_WD_FrameEnter($sSession, $sLevel)
$iErr = @error
If $iErr Then
$sMessage = 'Error occurred on "' & $sLevel & '" level when trying to entering frame'
Else
_WD_LoadWait($sSession, 0, $iTimeout, Default, $_WD_READYSTATE_Complete) ; wait until current frame is fully loaded
$iErr = @error
If $iErr And $iErr <> $_WD_ERROR_Timeout Then
$sMessage = 'Error occurred on "' & $sLevel & '" level when waiting for a browser page load to complete'
Else
$sCurrentBody_ElementID = _WD_ExecuteScript($sSession, "return window.document.body;", Default, Default, $_WD_JSON_Element)
$iErr = @error
If $iErr Then
$sMessage = 'Error occurred on "' & $sLevel & '" level when checking "document.body" ElementID'
Else
$s_URL = _WD_ExecuteScript($sSession, "return window.location.href", Default, Default, $_WD_JSON_Value)
$iErr = @error
If $iErr Then
$sMessage = 'Error occurred on "' & $sLevel & '" level when checking URL'
EndIf
EndIf
EndIf
EndIf
$vResult = $sLevel & '|' & $sLevel & '|' & $sFrameAttributes & '|' & $s_URL & '|' & $sCurrentBody_ElementID & '|' & $bIsHidden & '|' & @CRLF
If Not $iErr Then
Local $iFrameCount = _WD_GetFrameCount($sSession)
$iErr = @error
If $iErr Then
$sMessage = 'Error occurred on "' & $sLevel & '" level when trying to check frames count'
Else
Local $sFrameElementID
Local Const $sJavaScript_FrameAttributes = "function FrameAttributes(FrameIDX) { let nodes = document.querySelectorAll('iframe'); if (nodes.length) { return nodes[FrameIDX].outerHTML; } else { return window.frames[FrameIDX].frameElement.outerHTML; } }; return FrameAttributes(%s);"
Local Const $sJavaScript_FrameElementID = "function FrameElementID(FrameIDX) { let nodes = document.querySelectorAll('iframe'); if (nodes.length) { return nodes[FrameIDX]; } else { return document.querySelectorAll('frame')[FrameIDX]; } }; return FrameElementID(%s);"
For $iFrame = 0 To $iFrameCount - 1
If $sMessage Or $iErr Then ; message from last subframe is logged in the end of this function - not within For To Next loop
$_WD_DEBUG = $_WD_DEBUG_Saved ; turn off prevention for a moment
__WD_Error($sFuncName, $iErr, $sParameters & ' Information: ' & $sMessage) ; log messages which comes from loop processing
If $_WD_DEBUG <> $_WD_DEBUG_Full Then $_WD_DEBUG = $_WD_DEBUG_None ; again turn on prevention
EndIf
$sMessage = '' ; clear recent/previous message
$sFrameAttributes = _WD_ExecuteScript($sSession, StringFormat($sJavaScript_FrameAttributes, $iFrame), Default, Default, $_WD_JSON_Value)
$iErr = @error