Forum Discussion

azakharov's avatar
azakharov
Contributor
11 years ago

Waiting for XPath or CSS elements

Hi, What is the best strategy for waiting XPath and CSS elements to be displayed on a page? They are AJAX generated so I can't rely on page.Wait(). I need to achieve the following...
  • rgratis's avatar
    11 years ago

    I usually do a code loop until the object is found or until some sort of safety timeout period is exceeded.  I write in VBScript, but hopefully, you can see the intention and create something similar to suit your needs.




     



     'We'll wait for ten seconds


    curTime = aqDateTime.Now


    endTime = aqDateTime.AddSeconds(curTime, 10)




    'Start loop


    Do  



    'Optional delay to space out the attempts to find the element


    aqUtils.Delay 250



    ' QuerySelector will return object or Nothing/null

    Set elementOb = Page.QuerySelector("whatever")

     


    Loop Until Not elementOb Is Nothing Or aqDateTime.Compare(aqDateTime.Now, endTime) > -1

    ' Loops exits if the elementOb is a valid object (not Nothing) or we've passed the set end time



    If elementOb Is Nothing Then Log.Error "Failed to find element object."


     


    End Sub