等到特定元素加载 [英] Wait until specific elements loaded

查看:24
本文介绍了等到特定元素加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试抓取一个网站,我正在处理许多元素.我需要等待加载元素.

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?

这是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>

推荐答案

您可以将其缩短为

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

虽然你应该设置超时

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 

不会有错误,也不需要让出控制.如果未找到该项目,.Count 将为零.

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.

如果 id 是动态的并且您有一个常量子字符串(它只出现在页面上的一个 id 属性上(为了安全 - 可能不是必需的),您可以在 css 中使用 ^、*、$ 运算符).例如,如果开始字符串跨页面是常量,则将 css 选择器更改为

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)

这篇关于等到特定元素加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆