Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait until specific elements loaded

I am trying to scrape a website and there are many elements I am dealing with. I need to wait for elements to be loaded.

This is my try till now but I am waiting a long time and sometimes I get errors.

.FindElementById("ContentPlaceHolder1_Button1").Click
.Wait 2000
GoBack1:
Set elePrint = .FindElementById("IconImg_CrystalReportViewer1_toptoolbar_print", timeout:=20000, Raise:=False)
If elePrint Is Nothing Then
    Application.Wait Now() + TimeValue("00:00:01"): GoTo GoBack1
Else
    elePrint.Click
End If
GoBack2:
Set eleExport = .FindElementById("theBttnbobjid_1545642213647_dialog_submitBtn", timeout:=20000, Raise:=False)
If eleExport Is Nothing Then
    Application.Wait Now() + TimeValue("00:00:01"): GoTo GoBack2
Else
    eleExport.Click
End If

Is there a better way for doing this?

This is the html part

<tbody><tr valign="middle"><td height="21" width="5" style="background-image:url('aspnet_client/system_web/4_0_30319/crystalreportviewers13/js/crviewer/../dhtmllib/images/skin_standard/button.gif');background-position:0px 0px;"></td><td id="theBttnCenterImgbobjid_1545656314367_dialog_submitBtn" align="center" class="wizbutton" style="padding-left:3px;padding-right:3px;background-image:url('aspnet_client/system_web/4_0_30319/crystalreportviewers13/js/crviewer/../dhtmllib/images/skin_standard/button.gif');background-position:0px -42px;"><nobr><a id="theBttnbobjid_1545656314367_dialog_submitBtn" href="javascript:void(0)" class="wizbutton" role="button">Export</a></nobr></td><td height="21" width="5" style="background-image:url('aspnet_client/system_web/4_0_30319/crystalreportviewers13/js/crviewer/../dhtmllib/images/skin_standard/button.gif');background-position:0px -21px;"></td></tr></tbody>
like image 826
YasserKhalil Avatar asked Jun 29 '26 19:06

YasserKhalil


2 Answers

You can try looping your elements until they become available.

On Error Resume Next
Do While .FindElementById("theBttnbobjid_1545642213647_dialog_submitBtn") Is Nothing
    DoEvents
Loop
On Error Goto 0

I would also consider adding a timer to your loop if for some reason the the webpage hangs - which if this is the case you could reload the webpage or perform some other error handling operation.

like image 86
K.Dᴀᴠɪs Avatar answered Jul 02 '26 08:07

K.Dᴀᴠɪs


You can shorten this to

Do 
Loop While .FindElementsByCss("#theBttnbobjid_1545642213647_dialog_submitBtn").Count = 0 

Though you should set a timeout

Const MAX_WAIT_SEC As Long = 10
Dim t 
t = Timer
Do 
    If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While .FindElementsByCss("#theBttnbobjid_1545642213647_dialog_submitBtn").Count = 0 

There won't be an error and there is no need to yield control. If the item is not found the .Count will be zero.

If the id is dynamic and you have a constant substring (which only occurs for one id attribute on the page (to be safe - may not be essential) you can use ^, *, $ operators in css). For example, if the start string is constant across pages change the css selector to

[id^='theBttnbobjid']

If occurs more than once, and the index is constant across pages then use the index to interact later e.g.

.FindElementsByCss("[id^='theBttnbobjid']")(2)
like image 41
QHarr Avatar answered Jul 02 '26 10:07

QHarr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!