PublicSub WaitForPageReady(ByRef aWebObj As Object, ByVal aWebStr As String, Optional ByVal aTimeOut As Long = 10) Dim vFrame As HTMLDocument Dim vElement As Object Dim vBuffer As Variant Dim vTimeLimit As Date vTimeLimit = DateAdd("s", aTimeOut, VBA.Now)
OnErrorResumeNext
SelectCase aWebStr Case"Page1" # depends on different kind of page, the "last load object"is different DoWhile vBuffer <> "theTarget" Set vFrame = aWeb.Object.Document Set vElement = vFrame.Document.GetElementsByClassName("some class name here") vBuffer = vElement.innerText # get the compare string IfDateDiff("s", vTimeLimit, VBA.Now) > 0Then Err.Raise # Add Err message here EndIf DoEvents Loop EndSelect
EXIT_CleanUp: OnErrorGoTo0 Err.Clear ExitSub ERR_CleanUp: OnErrorGoTo0 Err.Raise # Add Err message here ERR_Handler: # Store Err message Resume ERR_CleanUp: EndSub
在pass这两个function后就可以开始scripting网页上的内容了. 注意,在access 网页object的时候,除了class name,还有frame的区别. 需要在“可以读取的frame”内,access 相应的object,不然读不到. 辨别方式:在inspect,select element in the page to inspect看看是否有不同的frame.
在这里查看属于哪一个frame.
注意vFrame.Document.GetElementsByClassName -> 根据不同需求,可以用getElementById等其他function. Mentor的代码是自己搓了个类似WaitForBrowserReady的Function. 如果一定时间内getElement function没有get到target的内容,也time out 给error然后退出.