Forum Discussion

sunireddy2007's avatar
sunireddy2007
Occasional Contributor
11 years ago

Scripting and Finding DIV Elements

Hi,



I am creating a simple test script to test a web page. After logging into the webapp, the page displays a list of DIV items and each DIV contains a hyperlink. And also some other text with some identifying numbers. Using the page.FindAllChildren method and passing className of the DIV plus the tagName as DIV, I am able to get an array of DIV objects. Now I loop thru and look for my particular DIV with the number I am looking for. The script finds it successfully. Now when I call Find method on the div object passing in the contentText and tagname properties, I am not able to find the hyperlink. I am able to see the properties in the Object Spy and cannot get the link via script. Please help!!!



Below is the code after finding the particular DIV I am looking for. It always goes into the Else block below.




  hmap.Add "contentText", "Project Setup"


  hmap.Add "tagName", "A"


  hmap.Add "innerHTML", "Project Setup"


  hmap.Add "title", "Click to work on the task" 


  keys = hmap.Keys


  values = hmap.Items


  Set PrjLink = myDiv.Find(keys, values, 100)


  If (PrjLink.Exists) Then


    Log.Message "link exists"


   PrjLink.Click


  Else


    Log.Message "link does not exist, "


  End If



Sunitha.

8 Replies

  • Hi,



    Why don' t you try getting an array of hyperlinks.Hyperlinks also start with a specific tagname.





    -Neha
  • AlexKaras's avatar
    AlexKaras
    Community Hero
    Hi Sunitha,



    It is my guess that hmap is something like Dictionary object... but the .Find method requires either a string or an array as its parameters. So my suggestion will be to try this:


      Set PrjLink = myDiv.Find( _

        Array("contentText", "tagName", "innerHTML", "title"), _

        Array("Project Setup", "A", "Project Setup", "Click to work on the task"), _

        100)


      If (PrjLink.Exists) Then


        Log.Message "link exists"


       PrjLink.Click


      Else


        Log.Message "link does not exist, "


      End If

  • sunireddy2007's avatar
    sunireddy2007
    Occasional Contributor
    I know that there is only one hyperlink in the entire DIV. I looked at the innerHTML of each of the DIVs in Object Spy.
  • Hi,



    Can you please attach a text file containg the innerHtml.If its private then your own version with same structure and the link you want to fetch.



    Thanks,

    Neha
  • sunireddy2007's avatar
    sunireddy2007
    Occasional Contributor
    Hi Alex,



    Thanks for the reply. Yes hmap is a dictionary object. The Keys and Items methods return Array objects exactly like you suggested. I even tried what you suggested and still no luck.



    Sunitha.
  • dale_waterworth's avatar
    dale_waterworth
    Occasional Contributor
    Just use xpath - it's much simpler:



    you can say something like this psuedo



    //create your xpath 

    var xpath = "//*//div/a";



    //pass it into the evaluate xpath function where page is the browser object

    var elements = page.EvaluateXPath(xPath);



    for each 'a' element

    //do test





    this is cleaner, simpler and less of a headache.



    Once your familiar with it you can just create a function to evaluate any xpath and return your array of results.
  • sunireddy2007's avatar
    sunireddy2007
    Occasional Contributor
    I tried the xpath search too. The problem is that xpath is not working at all. Even the DIV items which I am able to find via Page.FindAllChildren method, I am not able to get the same using EvaluateXPath method. Not sure if there is any setting I need to make. One thing though is that when I do a view source on the page I dont see the DIV elements I am looking for in the html. Is that the reason? If Xpath works I feel that it is a better way to search for the elements because I can use search by partial text for prop values.
  • Hi,



    In page source links commonly have this structure:



    <a id="id" class="ActionLink" href="javascript&colon;modalDialog(&#39;/cms/dialogs/CMSModules/AbuseReport/CMSPages/ReportAbuse.aspx?params=64f2de3f-c8de-447f-b7f5-2e84f48fcec3&#39;, &#39;reportDialog&#39;, 425, 370);">Report abuse</a>

    You can use function like below to create an Array of links:



    Function Category(id,value,spattern,epattern)


    set window = getBrowser.Page("*")


     


    Str_html = window.NativeWebObject.Find(id,value).innerHtml


    int_start = 1


    int_start1 = 1


    i = 0


    cat_name0 = Array()


     


    Do Until ( InStr(int_start,str_html,spattern) = 0)


    ReDim Preserve cat_name0(i)


     


    int_start = instr(int_start,str_html,spattern)


    int_start = int_start + len(spattern)


     


    int_start1 = instr(int_start,str_html,epattern)


    cat_name0(i) = mid(str_html,int_start,int_start1-int_start)


    Log.message(cat_name0(i))


    i = i+1


    Loop


    End Function




    Where spattern can be : ">

    and epattern can be :
    </a>



    -Neha