gpt4 book ai didi

vba - 无法在我的刮刀中设置超时选项以使其免于无限循环

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

我在 vba 中编写了一个脚本,使用 IE 在其搜索框中的网页中启动搜索,通过点击搜索按钮根据搜索填充结果。网页加载它是searchbox几秒钟后,它被打开了。但是,我下面的脚本可以处理这个障碍并以正确的方式执行搜索。

现在,我有一个稍微不同的问题:假设我定义了一个错误的 ID名称或没有这样的ID在那个网页中,我编写脚本的方式将永远循环。这当然是一个严重的问题。

如何修改我的脚本,添加一些 timeout循环中的选项,以便爬虫将等待某个时间元素出现,如果该元素在该时间内不存在,则它将跳出循环并退出浏览器。提前致谢。

这是我的脚本:

Sub GetItems()
Dim HTML As HTMLDocument, post As Object

With New InternetExplorer
.Visible = True
.navigate "https://torrentz2.eu/"
While .Busy Or .ReadyState < 4: DoEvents: Wend
Set HTML = .Document

With HTML
Do ''Wish to set timeout options within this loop to save it from infinite looping
Set post = .getElementById("thesearchbox")
DoEvents
Loop While post Is Nothing

post.innerText = "Udemy"

.getElementById("thesearchbutton").Click
End With
.Quit
End With
End Sub

最佳答案

尝试更换:

    With HTML
Do ''Wish to set timeout options within this loop to save it from infinite looping
Set post = .getElementById("thesearchbox")
DoEvents
Loop While post Is Nothing

post.innerText = "Udemy"

.getElementById("thesearchbutton").Click
End With


Dim dTime as Single ' as pointed out by @Mathieu Guindon in comments
'____________________________

dTime = Timer

With HTML
Do ''Wish to set timeout options within this loop to save it from infinite looping
Set post = .getElementById("thesearchbox")
If Timer - dTime > 60 Then Exit Do ' 60: num of seconds, equal 1 min
DoEvents
Loop While post Is Nothing

post.innerText = "Udemy"

.getElementById("thesearchbutton").Click
End With

关于vba - 无法在我的刮刀中设置超时选项以使其免于无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50377756/

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