Unable to check for availability of any element re

2019-03-04 04:28发布

I've written a script in vba in association with selenium to initiate a search in some torrent site. My script is doing fine but the problem is I had to use hardcoded delay within my script to make it successful. What I wish to do now is check for the availability of desired element using some loop or any of that sort by kicking out hardcoded delay from my script. Any help on this will be highly appreciated.

This is my attempt so far (working one):

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

Reference to add:

Selenium Type Library

1条回答
女痞
2楼-- · 2019-03-04 04:50

Seems to have found the solution already. There is the link leading to github where Florent B. has provided an excellent solution as to how the script should wait until the desired element is available.

This is how the script should be:

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
查看更多
登录 后发表回答