Forum Discussion

sean_ng's avatar
sean_ng
Occasional Contributor
6 years ago
Solved

search object with dynamic id using find Child By X Path

hi, i am using ChildByXPath function to look for object on web but i am having problem because the id is dynamic.

id = ABC-60 the value 60 will keep changing on every click.

what i want to do is, find the object using only ABC- and leave the value behind blank and when test complete search for the object it will search for every object that contain ABC-

so regardless of what value is after ABC- test complete will be able to click on it.

 

How can i do that ? Below is my code :

 

var obj= ChildByXPath("//*[@id=\"ABC-60\"]");




function ChildByXPath(xpath)
 {
    var page = Sys.Browser("Chrome").Page("*");
    var obj = page.FindChildByXPath(xpath, true);
  if (obj != null)
    {
      return obj;
   }
    else
    { 
      Log.Error("The element was not found.");
    }
 }

 

  • sean_ng's avatar
    sean_ng
    6 years ago

    Hi AlexKaras,

     

    Thank you for your reply.

    i was having trouble using findchild function, i know and understand that you advice me to avoid using xPath but i found one code that is working for my case. I'll try my best to avoid xPath for test complete in future.

     

    Below, i would like to share the code that i am using ..

     

    var obj = ChildByXPath("//*[starts-with(@id, 'ABC-')]");
    
    function ChildByXPath(xpath)
     {
        var page = Sys.Browser("Chrome").Page("*");
        var obj = page.FindChildByXPath(xpath, true);
      if (obj != null)
        {
          return obj;
       }
        else
        { 
          Log.Error("The element was not found.");
        }
     }

2 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    While searching by XPath is extremely popular in Selenium world, I would recommend to avoid it as much as possible in the world of TestComplete and use .FindChild() and/or .FindAllChildren() methods instead. They support wildcards and are more performant when compared to XPath search.

    See TestComplete documentation for the description and code samples for the mentioned methods.

     

    • sean_ng's avatar
      sean_ng
      Occasional Contributor

      Hi AlexKaras,

       

      Thank you for your reply.

      i was having trouble using findchild function, i know and understand that you advice me to avoid using xPath but i found one code that is working for my case. I'll try my best to avoid xPath for test complete in future.

       

      Below, i would like to share the code that i am using ..

       

      var obj = ChildByXPath("//*[starts-with(@id, 'ABC-')]");
      
      function ChildByXPath(xpath)
       {
          var page = Sys.Browser("Chrome").Page("*");
          var obj = page.FindChildByXPath(xpath, true);
        if (obj != null)
          {
            return obj;
         }
          else
          { 
            Log.Error("The element was not found.");
          }
       }