Search code examples
excelvbaseleniumwebdriver

Selenium Webdriver (VBA): Explicit Wait


I am navigating a web application that will often throw an error if there is an attempt to click an element before it can be interacted with.

When using Selenium WebDriver (java), I can easily work around the problem:

 WebDriverWait wait = new WebDriverWait(driver, 15);

 wait.until(ExpectedConditions.elementToBeClickable(By.id("element")));

However, I am trying to write the script in VBA utilizing the Selenium type library, and, despite trying numerous different ways, the only success I am having is:

webdriver.wait

which I have been told should be avoided if at all possible. If someone can advise how to translate my java into VBA, or provide any other solution, I would be extremely grateful.


Solution

  • The selenium plugin for VBA is unofficial and doesn't support this feature.

    You can work around this by using onError to retry the action that is producing an error until it succeeds or times out:

    Sub test
        OnError GoTo Retry
            webDriver.findElementById("element")
        Exit Sub
    
        Dim i as integer
        :Retry
            webDriver.Wait(500)
            i = i + 1
            if i = 20 then onerror go to 0
        Resume
    end sub