gpt4 book ai didi

html - 代码在 F8 单步执行期间有效,但在正常运行时无效

转载 作者:行者123 更新时间:2023-12-04 21:54:43 25 4
gpt4 key购买 nike

Sub InternetPractice()

Dim ie As InternetExplorer

Set ie = New InternetExplorer

ie.Visible = True

ie.navigate "https://www.yahoo.com"

Do While ie.Busy = True Or ie.readyState <> 4: DoEvents: Loop

ie.document.getElementById("uh-search-box").Value = "Earth"
ie.document.getElementById("uh-search-button").Click

Do While ie.Busy = True Or ie.readyState <> 4: DoEvents: Loop

ie.document.getElementById("logo").Click

Set ie = Nothing

End Sub

该代码在我逐行执行时有效,但在正常运行时无效。我尝试在某些部分插入长达 45 秒的休息时间,但没有成功。

正常运行, ie.document.getElementById("logo").Click期间代码中断并给了我一个

'424' object required error

最佳答案

因为这是对象尚不可用的问题,解决问题的最简单方法是设置 logo首先作为一个对象 - 循环直到它是 Not Nothing ,然后单击它。

Option Explicit

Sub InternetPractice()

Dim ie As New InternetExplorer, logoBtn As Object

With ie
.Visible = True
.navigate "https://www.yahoo.com"
Do While .Busy Or .readyState <> 4: DoEvents: Loop
.document.getElementById("uh-search-box").Value = "Earth"
.document.getElementById("uh-search-button").Click
Do While .Busy = True Or .readyState <> 4: DoEvents: Loop
On Error Resume Next
Do While logoBtn Is Nothing
Set logoBtn = .document.getElementById("logo")
DoEvents
Loop
On Error Goto 0
End With
logoBtn.Click

End Sub

因此,您实际上是在替换这一行: ie.document.getElementById("logo").Click
有了这个:
    Dim logoBtn As Object
Do While logoBtn Is Nothing
Set logoBtn = ie.document.getElementById("logo")
DoEvents
Loop
logoBtn.Click

即使在 readyState = Complete 之后,在某些情况下,对象没有完全初始化。这可能部分是由于除了您看到的主 HTML 页面之外,还有多个 iFrame 可以做自己的事情。

关于html - 代码在 F8 单步执行期间有效,但在正常运行时无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47185605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com