Forum Discussion

velise's avatar
velise
Occasional Contributor
12 years ago

Get body of a web page with Iexplore

Hello,



I'm using Test Complete 8

and tring to get body of a web page by scripting.



I tried to use :

Sub Test

  Dim url, browser, body, str

  url = "http://www.google.fr"

  Set browser = Sys.Process("iexplore").ToURL(url) 

  Set body = browser.contentDocument.body

  Log.Message(body.textContent)

End Sub



If i use firefox instead of iexplore it works fine but if i'im usig IE8 i have this error :

Unable to find the object contentDocument. See Additional Information for details. 14:03:28 Normal  



Anyone can explain me why ?

6 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Yvelise,



    The contentDocument property is only available for Internet Explorer web pages starting from TestComplete 9. In earlier TestComplete versions, you need to use the Application.Document property to access the DOM document object.



    Moreover, Internet Explorer doesn't natively support the textContent property prior to version 9, so you should use innerText instead. However, please be aware of a few differences between innerText and textContent.



    You'll need to check the current browser and use the appropriate properties, like this:

    Sub Test

      Dim url, browser, page, body, str



      url = "http://www.google.fr"

      strBrowser = "iexplore"



      Set browser = Sys.Process(strBrowser)

      Set page = browser.ToURL(url)



      If LCase(browser.ProcessName) = "iexplore" Then

        Set body = page.Application.Document.body

        str = body.innerText

      Else ' Firefox & Chrome

        Set body = page.contentDocument.body

        str = body.textContent

      End If



      Log.Message "See Additional Info.", str 

    End Sub
  • velise's avatar
    velise
    Occasional Contributor
    Hello,



    Thank's a lot for your reply.

    Do you know if it's possible to get the source code of an page already loaded (a page that is loaded after a click action) ?



    I try to explain myself :

    Instead of using Set page = browser.ToURL(url) 

    Is it possible to use something like 

    Set page = browser.Page(url) 

    I know this one doesn't work...



    Thank's again
  • velise's avatar
    velise
    Occasional Contributor
    here is how i test my code :

    1) I open iexplore

    2) Manually open http://www.google.fr

    3) Once page is loaded i run my script :




    Sub Find_Str_Body

      Dim url, browser, page, body, str  

     

      url = "http://www.google.fr"

      strBrowser = "iexplore"    

      str = "Recherche"  

      

      Set browser = Sys.Process(strBrowser) 

      Set page = browser.Page(url) 

      

      If LCase(browser.ProcessName) = "iexplore" Then

        Set body = page.Application.Document.body  

        if(aqObject.CheckProperty(body, "innerText", cmpContains, str, False) ) then

              Log.Message("Texte trouve")

       else

             Log.Message("Texte non trouve")      

       end if

      Else ' Firefox & Chrome

        Set body = page.contentDocument.body

        if( aqObject.CheckProperty(body, "textContent", cmpContains, str, False) ) then

          Log.Message("Texte trouve")

        else

          Log.Message("Texte non trouve")      

        end if

      End If

          

    End Sub 


    4) i haver the error : 

    Unable to find the object Page(http://www.google.fr). See Additional Information for details. 12:11:04 Normal

    on line   Set page = browser.Page(url)
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Yvelise,



    You need a trailing slash in the URL: