1

当前代码如下:

Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "https://www.website.com/"
Application.StatusBar = "https://www.website.com/ is loading. Please wait..."
Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now)
    Loop
Application.StatusBar = "Search form submission. Please wait..."

我遇到的问题是“ https://www.website.com/ ”有时无法加载(我相信这是由于我反复访问它所致)。结果是代码永远不会超出这个循环:

Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now)
    Loop

在我看来,一个合乎逻辑的解决方案是在达到一定的时间限制(30 秒?)后杀死 IE 并重新启动整个进程。请就如何编写此解决方案和/或如何编写更有效的解决方案提供建议。非常感谢你!我正在使用 IE10,如果这很重要的话。

另外,这是我发现的一个链接,其中包含一些相关和相关的信息: http ://www.ozgrid.com/forum/showthread.php?t=161094

4

3 回答 3

5

您可以尝试修改代码以阅读

Sub LoadWebsite()
Dim iSecondCounter As Integer
Static iAttempts As Integer

iAttempts = iAttempts + 1

Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "https://www.website.com/"
Application.StatusBar = "https://www.website.com/ is loading. Please wait..."

iSecondCounter = 0
Do While ie.Busy And iSecondCounter < 30
    Application.Wait DateAdd("s", 1, Now)
    iSecondCounter = iSecondCounter + 1
Loop
If iSecondCounter = 30 Then
    ie.Quit
    If iAttempts <= 3 Then Call LoadWebsite
End If

Set ie = Nothing
End Sub
于 2013-08-16T03:07:02.003 回答
0

好的,您似乎在问一个关于获取 HTML 元素集合的新问题,所以我将其作为新答案发布。您收到对象错误的原因似乎是因为您没有将 objCollection 标注为正确的对象。我已经修改了原始答案中的代码来执行此操作,并将集合设置为您的代码段中。要使用此代码,我必须包含对 Microsoft HTML 对象库的引用。

请注意,此代码在退出子程序之前破坏了元素集合,您可能想对它做点什么。

Sub LoadWebsite()
Dim iSecondCounter As Integer
Static iAttempts As Integer
Dim objCollection As IHTMLElementCollection
iAttempts = iAttempts + 1

Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "https://www.website.com/"
Application.StatusBar = "https://www.website.com/ is loading. Please wait..."

iSecondCounter = 0
Do While ie.Busy And iSecondCounter < 30
    Application.Wait DateAdd("s", 1, Now)
    iSecondCounter = iSecondCounter + 1
Loop
If iSecondCounter = 30 Then
    ie.Quit
    If iAttempts <= 3 Then Call LoadWebsite
End If


Set objCollection = (ie.Document.getElementsByTagName("INPUT"))

Cleanup:

Set ie = Nothing
Set objCollection = Nothing

End Sub
于 2013-08-16T22:44:11.493 回答
0

也许您可以添加代码来帮助您等待它加载,并且在 X 秒后您可以决定是否等待,或者您可以优雅地退出/做其他事情(发布消息等)

ie.Navigate "http://www.website.com"
Application.Wait Now + TimeSerial(0, 0, 3)

Do While ie.Busy
    DoEvents
Loop
于 2013-08-17T01:03:09.710 回答