excel - 等待特定元素加载

标签 excel vba selenium-webdriver web-scraping

我正在尝试抓取一个网站,并且我正在处理很多元素。我需要等待元素加载。

这是我到目前为止的尝试,但我等待了很长时间,有时会出现错误。

.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/

相关文章:

vba - 加载图像使列表框和文本框闪烁一次

java - 如何使用 selenium webdriver 在 JQGrid 中选择单选按钮

java - 可克隆和 WebDriver ( Selenium )

vba - 在列表框vba中找到所选项目的行#

search - Excel VLOOKUP 和 SEARCH 组合

Python:导出 Pandas 数据框时如何在 Excel 中创建多行单元格

VBA Application.Caller 运行时错误

vba - 比较 Excel 2010 中的值和转移列

excel - 将选定的电子表格从 Excel 文件导入到 Access 2007 表中的 VBA 代码是什么

python - 使用 Python 和 Selenium Webdriver 存储动态下拉选项