我正在尝试抓取一个网站,并且我正在处理很多元素。我需要等待元素加载。
这是我到目前为止的尝试,但我等待了很长时间,有时会出现错误。
.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
有更好的方法吗?
这是html部分
<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>
最佳答案
您可以尝试循环元素,直到它们可用。
On Error Resume Next
Do While .FindElementById("theBttnbobjid_1545642213647_dialog_submitBtn") Is Nothing
DoEvents
Loop
On Error Goto 0
如果由于某种原因网页挂起,我还会考虑在循环中添加一个计时器 - 如果是这种情况,您可以重新加载网页或执行一些其他错误处理操作。
关于excel - 等待特定元素加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53911498/