首页 > 解决方案 > 无法检查移除硬编码延迟的任何元素的可用性

问题描述

我在 vba 中编写了一个与 selenium 相关的脚本,以在某些 torrent 站点中启动搜索。我的脚本运行良好,但问题是我必须hardcoded delay在脚本中使用才能使其成功。我现在想做的是通过从我的脚本中剔除硬编码延迟来使用某个循环或任何一种循环来检查所需元素的可用性。对此的任何帮助将不胜感激。

这是我迄今为止的尝试(工作之一):

Sub SearchItem()

    With New ChromeDriver
        .get "https://torrentz2.eu/"

        Application.Wait Now + TimeValue("00:00:10")  ''I wish to shake this hardcoded delay off
        .FindElementByCss("#thesearchbox").SendKeys ("Udemy")
        .FindElementByCss("#thesearchbutton").Click
    End With
End Sub

参考添加:

Selenium Type Library

标签: vbaexcelseleniumweb-scrapingselenium-chromedriver

解决方案


似乎已经找到了解决方案。有指向github的链接Florent B.提供了一个很好的解决方案,说明脚本应该如何等待所需的元素可用。

脚本应该是这样的:

Sub SearchItem()

    With New ChromeDriver
        .get "https://torrentz2.eu/"
        .FindElementByCss("#thesearchbox", timeout:=10000).SendKeys "Udemy"  ''wait for the element upto 10 seconds
        .FindElementByCss("#thesearchbutton").Click
    End With
End Sub

推荐阅读