Forum Discussion

Alison_Hawke's avatar
Alison_Hawke
Occasional Contributor
14 years ago

Search for an element within a DIV on a web page

Our application is large, and Page.NativeWebObject.Find("id", "myID") is crushingly slow.  I need to narrow down the search to a specific DIV within the web page.



I've tried this:




function search()


search()

{


// this will always find the first link


var app, testfile, myPage, myLink;


 


app = TestedApps.iexplore.Run();


testfile = "file:///" + Project.Path + "search-test.html";


Log.Message(testfile);


myPage = app.ToURL(testfile);


 


// search for the element ID "muppet" and click it


myLink = myPage.NativeWebObject.Find("id", "muppet");


if (myLink.Exists)


    myLink.Click();


else


    Log.Warning("No link here");


}



What I want to do is search for a link within a DIV with the ID "second_section" (<div id="second_section">)



How do I do this, in vbscript and in jscript, for IE7 and IE8?  I've been looking at the FindChild method, and the Find method, and the documentation doesn't help.

8 Replies

  • Alison_Hawke's avatar
    Alison_Hawke
    Occasional Contributor
    This is what I tried next, and it doesn't work:



    function search_in_a_div()

    {

        var app, testfile, myPage, mySection, myLink;

        var PropArray, ValuesArray;

        var ConvertedPropArray, ConvertedValuesArray;

       

        app = TestedApps.iexplore.Run();  

        testfile = "file:///" + Project.Path + "search-test.html";

        Log.Message(testfile);

        myPage = app.ToURL(testfile);

       

        // search for the link "Click" and click it

        mySection = myPage.NativeWebObject.Find("id", "second_section");

        if (mySection.Exists)

        {

            Log.Message("Found second_section");

           

            // now search WITHIN this DIV for an element with the id of "muppet"

            PropArray = new Array("div", "id");

            ValuesArray = new Array("muppet", true);

            // Converts arrays

            ConvertedPropArray = ConvertJScriptArray(PropArray);

            ConvertedValuesArray = ConvertJScriptArray(ValuesArray);

           

            myLink = mySection.Find(ConvertedPropArray, ConvertedValuesArray, 5);

            if (myLink.Exists)

                myLink.Click();

            else

                Log.Warning("No link here");

        }             

        else

            Log.Warning("No second_section here");

    }


    function ConvertJScriptArray(AArray)

    {

        // Uses the Dictionary object to convert a JScript array

        var objDict = Sys.OleObject("Scripting.Dictionary");

        objDict.RemoveAll();

       

        for (var j in AArray)

            objDict.Add(j, AArray);

           

        return objDict.Items();

    }

  • Hi Alison,


    The general approach you should use is: 1) obtain the needed DIV element (it might be faster to use the FindChild method here too, depending on the objects hierarchy) 2) use the FindChild method to obtain the link with the specified properties, which is a child of the DIV element.


    I guess the cause of the problem is the parameters you pass to the FindChild method:



    PropArray = new Array("div", "id");

    ValuesArray = new Array("muppet", true);



    The parameters mean the following: "find an element with the "div" property equal to "muppet" and the "id" property equal to true". This does not look like valid object recognition criteria. You need to use property/value pairs you see in the Object Browser.

  • Alison_Hawke's avatar
    Alison_Hawke
    Occasional Contributor
    I replaced the Array statements with this:



    PropArray = new Array("ObjectType", "id");

    ValuesArray = new Array("Link", "muppet");



    According to your explanation, this is now searching for an object type of Link (from the Object browser) that has an id of "muppet".



    I get the error "Object required" for the PropArray line.  What is wrong with this code?
  • Alison_Hawke's avatar
    Alison_Hawke
    Occasional Contributor
    I have sent in both a VB and  a Java version of the project.  Please let me know what the fix is for both versions.

  • Hi Alison,





    We have received the files, thanks. Allen will get back to you soon.
  • Hi Alison,


    The problem is caused by the fact that the Hybrid tree model is used in your project, and as a result, the NativeWebObject.Find method returns an object from the DOM branch of the objects tree. The Hybrid model is actually used for compatibility purposes so projects created in earlier versions of TestComplete can run smoothly. I recommend that you just make your projects use the Tree model - this will solve the problem. Moreover, the Tree model is faster than the Hybrid model. Please see the "Web Tree Models" help topic ( http://www.automatedqa.com/support/viewarticle.aspx?aid=12439 ) for more information.


    Besides that, there is one more problem in your test: the "id" property used for object identification in the FindChild method must be replaced with the "idStr" property:



    PropArray = new Array("ObjectType", "idStr");

    ValuesArray = new Array("Link", "muppet");

  • Alison_Hawke's avatar
    Alison_Hawke
    Occasional Contributor
    Thanks for looking over the files Allan, that seems to have solved the problem.