Forum Discussion

Adagio's avatar
Adagio
Frequent Contributor
9 years ago

Using Xpath to identify Web Objects

Hello,

 

 I'm using  FindChildByXPath method to locate the elements using Xpath. I have to do this because these are custom controls, and mapping doesn't work. With all that, I have been encountering one issue quite often.

 

When I run the Tests, there are instances where the Test would fail because it's not able to locate that element, and at other instances, it just works fine. So, It's becoming a bit unreliable for me to use Xpaths. It could be because of the slowness in finding out that Object using the Xpaths. Does anyone have any solution/suggestions here?

 

Thank you

Abhi

1 Reply

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    It could also be a timing issue as well.  If you're searching for the object via XPath but the object hasn't actually been loaded, etc., on the page yet, this could be what you're experience.  Intermittent identification issues in my experience have to do with this sort of timing situation.

     

    Basically, a rule of thumb I enforce in our own environment is that ANY time something causes a page to reload or refresh, I always follow it with a Page.Wait method call... ALWAYS.  

     

    Now, in some more responsive web solutions, an action may not cause a full page load but there may still be objects that you may need to wait on before you proceed.  Something you might need to do is build your own custom "wait" function for some of these things.  When I've done such things in the past, I wrap my "Find" method in a while loop which loops for a parameterized number of times.  Within the loop, as mentioned, I put my find function and a brief delay (500 ms for example).  The condition for the while is "while the object does not exist" or "while the object is null" or "while the object variable is undefined" (pick one or more of these to include in the condition).  I wrap all this in a function that returns the object itself if it is found or an empty stub object with Exists = False if it fails to find it in the time frame.

     

    I know a rather long winded response, but I think, since you're using a lot of "Find" calls, this might be the way you want to go.