Forum Discussion

funmay's avatar
funmay
Contributor
7 years ago

Loop through the container of elements

I am new to Testcomplete with JavaScript.How can i loop through a collection/container of element?When all the elements are using the same class name/Id.I don't want run the test individually by using "contains". I want to loop through the container .

 

In Selenium i can do it with below code:

 

List<WebElement> vContainer = element.findElements(By.cssSelector("element));

 

        if (vContainer .size() > 0) {

            //Can iterate the list if you expect more than one div of type someDiv.

            String myText = mightHaveSomeDiv.get(0).getText();

                return element;

 

9 Replies

  • kaiiii's avatar
    kaiiii
    Regular Contributor

    We can find the element by using XPath

    page.FindChildByXPath("//TagName[@PropertyName='KeyValue'][text()]")
    Make an object for the above xpath and I hope it will be work.

    • funmay's avatar
      funmay
      Contributor

      Hi All,

      Thank you for the feedback.This is not working for me,when i used the FindChildByXPath,it is only return the first element,when it should return  5 elements with the below code.

       

      var containerEle;

      var ele = "//div[@class='something']/h3";
      var elepath= ObjectConverted(ele);

      containerEle = page.FindChildByXPath(tablexPath).innerText;
      for(i=0; i<=containerEle.length;i++){

      Log.MessagecontainerEle [i]);

      }

       

      Error seen(Wrong number of arguments or invalid property assignment 12:06:36 Normal Error) with FindAllChildren with the below code:

      var containerEle;

      var ele = "//div[@class='something']/h3";
      var elepath= ObjectConverted(ele);

      containerEle = pageFindAllChildren(tablexPath).innerText;
      for(i=0; i<=containerEle.length;i++){

      Log.MessagecontainerEle [i]);

      }

       

      Could you please provide code to make this action funtioned,this is very easy to do with Selenium.Can someone from SmartBear TestComplete help in this situation

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi,

         

        Unlike Selenium;), TestComplete provides really useful and descriptive documentation with a lot of code samples. So it's definitely worth reading.

        Your code is incorrect almost everywhere :)

        FindChildByXPath returns first found element, thus it is not possible to iterate through them. EvaluateXPath() returns an array of found elements and it is possible to iterate through the returned array.

        FindAllChildren() returns an array and thus it does not contain .innerText property. Also, FindAllChildren() expects not XPath but a different set of parameters.

         

        Final note: while it is possible to search elements in TestComplete via XPath and this may be more convenient for you after Selenium, more effective approach in TestComplete's world is to use Aliases, FindChild() and FindAllChildren() methods. I would recommend you to read about them in the help.

  • The language is set to JavaScript.I have used with toArray()method , but unable to resolve it.

    • tristaanogre's avatar
      tristaanogre
      Esteemed Contributor

      As suggested above... rather than using XPath, have you tried using FindAllChildren and using that to iterate through the desired elements?