[Solved] how to retrieve the HTML code from IE

Just starting out? Need help? Post your questions and find answers here.
Christophe_fr
User
User
Posts: 13
Joined: Thu Dec 13, 2018 9:58 pm
Location: france

[Solved] how to retrieve the HTML code from IE

Post by Christophe_fr »

Hello, I can get the html code of Internet Explorer when it is opened by the program. but how to do it when Internet Explorer is already open?
thank you

Code: Select all

;***COMate***  COM automation through iDispatch.
;*===========
;*
;*Web gadget demo. (Advanced users!)  Based upon code by hm.
;*
;*This demo not only shows how to obtain a COMateObject from a web-gadget, but also sets a 'global' event handler.
;*
;*This demo also shows how to over-ride the COMateObject SetEventHandler() method and direct COMate to seek out a specific
;*'out-going interface'. Left to it's own devices, COMatePLUS will utilise the first suitable 'connection point' and out-going interface
;*that it encounters. In the case of a web gadget, we direct COMatePLUS to directly seek out a connection point for the newer DWebBrowserEvents2
;*interface.

;*I did state that this demo is for advanced users!
;/////////////////////////////////////////////////////////////////////////////////


IncludePath "..\"
XIncludeFile "COMatePLUS.pbi"

Global browser.COMateObject
Global document.COMateObject
Global frames.COMateObject

Global eventName$
Declare GetHtml()




;/////////////////////////////////////////////////////////////////////////////////
;The following is our event callback for our web-gadget object.
;The 'Object' parameter will contain the COMate object upon which was used the \SetEventHandler() method; in our case it will point
;to our browser object.
Procedure EventProc(Object.COMateObject, eventName$, parameterCount) 
  Debug eventName$
  
EndProcedure 
;/////////////////////////////////////////////////////////////////////////////////


Procedure GetHtml()
  document = browser\GetObjectProperty("Document") : Debug "document :"+document
  If document
    frames = document\GetObjectProperty("frames") : Debug "frames :"+frames 
    If frames
      Debug document\GetStringProperty("Head\InnerHtml")
      Debug document\GetStringProperty("Body\InnerHtml")
    EndIf
  EndIf
  
EndProcedure


If OpenWindow(0, #PB_Ignore, #PB_Ignore, 130, 60, "WebGadget Events Test",  #PB_Window_SystemMenu | #PB_Window_TitleBar ) 
  StickyWindow(0,1)
  ButtonGadget(1, 10, 10, 110, 40, "GetHTML")
  
;   hwnd = FindWindow_("IEFrame",0) : Debug hwnd
;   browser = COMate_WrapCOMObject(GetWindowLongPtr_(hwnd, #GWL_USERDATA)) 
  
  browser = COMate_CreateObject("InternetExplorer.Application")
  browser\SetProperty("Visible = #true")
  browser\invoke("Navigate('http://www.purebasic.com')")
  
  Debug browser
  
  ;Set the 'global' event handler for the browser object.
  ;In order to over-ride COMatePLUS' selection of 'connection point' we pass a pointer to the IID of the DWebBrowserEvents2 out-going interface
  ;within the second optional parameter.
  
  If COMate_GetIIDFromName("DWebBrowserEvents2", @iid.IID) = #S_OK
    browser\SetEventHandler(#COMate_CatchAllEvents, @EventProc(), 0, iid)
  EndIf
EndIf


Repeat
Until browser\GetIntegerProperty("ReadyState") = 4




If browser 
  document = browser\GetObjectProperty("Document") : Debug "document :"+document
  If document
    frames = document\GetObjectProperty("frames") : Debug "frames :"+frames
    If frames
      Debug document\GetStringProperty("Head\InnerHtml")
      Debug document\GetStringProperty("Body\InnerHtml")
    EndIf
  EndIf
EndIf


Repeat 
  EventID = WaitWindowEvent() 
  
  Select EventID
    Case #PB_Event_Gadget 
      Select EventGadget()
        Case 1
          GetHtml()
      EndSelect
  EndSelect
  
Until EventID = #PB_Event_CloseWindow

frames\Release()

document\Release()

browser\Release()
sorry for my english, I'm french
Last edited by Christophe_fr on Wed Feb 20, 2019 1:06 pm, edited 1 time in total.
Sorry for my English. My language is French
User avatar
Derren
Enthusiast
Enthusiast
Posts: 313
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

Re: how to retrieve the HTML code from IE

Post by Derren »

rgose two lines, that were commented out?

Code: Select all

;   hwnd = FindWindow_("IEFrame",0) : Debug hwnd
;   browser = COMate_WrapCOMObject(GetWindowLongPtr_(hwnd, #GWL_USERDATA)) 
  
  browser = COMate_CreateObject("InternetExplorer.Application")
To this:

Code: Select all

   hwnd = FindWindow_("IEFrame",0) : Debug hwnd
   browser = COMate_WrapCOMObject(GetWindowLongPtr_(hwnd, #GWL_USERDATA)) 
  
;  browser = COMate_CreateObject("InternetExplorer.Application")
:?:
Christophe_fr
User
User
Posts: 13
Joined: Thu Dec 13, 2018 9:58 pm
Location: france

Re: how to retrieve the HTML code from IE

Post by Christophe_fr »

the two commented lines, it's my approach to use Internet Explorer already launched

link to COMatePlus
https://www.rsbasic.de/backupprogramme/COMatePLUS.zip
Sorry for my English. My language is French
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: how to retrieve the HTML code from IE

Post by Marc56us »

Hello, I can get the html code of Internet Explorer when it is opened by the program. but how to do it when Internet Explorer is already open?
; hwnd = FindWindow_("IEFrame",0) : Debug hwnd
; browser = COMate_WrapCOMObject(GetWindowLongPtr_(hwnd, #GWL_USERDATA))
the two commented lines, it's my approach to use Internet Explorer already launched
FindWindow_ is an API that search for an opened window, so if you remove it, your program will never search for window :wink:
Christophe_fr
User
User
Posts: 13
Joined: Thu Dec 13, 2018 9:58 pm
Location: france

Re: how to retrieve the HTML code from IE

Post by Christophe_fr »

FindWindow_ is an API that search for an opened window, so if you remove it, your program will never search for window :wink:
if I commented the two lines is to show that my program works when it opens itself Internet Explorer.
But when I search the window with FindWindow_ it does not work
Sorry for my English. My language is French
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: how to retrieve the HTML code from IE

Post by mk-soft »

Perhaps over ActiveScript

Code: Select all

;-TOP

; Comment   : Modul ActiveScript Example 4

; Link to ActiveScript  : https://www.purebasic.fr/english/viewtopic.php?f=12&t=71399
; Link to SmartTags     : https://www.purebasic.fr/english/viewtopic.php?f=12&t=71399#p527089
; Link to VariantHelper : https://www.purebasic.fr/english/viewtopic.php?f=12&t=71399#p527090

; ***************************************************************************************

XIncludeFile "Modul_ActiveScript.pb"
;XIncludeFile "Modul_SmartTags.pb"
;XIncludeFile "VariantHelper.pb"

UseModule ActiveScript
;UseModule ActiveSmartTags

; -------------------------------------------------------------------------------------

Global vbs.s, result.s

; Variable als Rumtime definieren zum Zugriff aus VB-Script
Runtime result

; Daten anlegen

; VB-Script schreiben
vbs = ~""
vbs + ~"Dim IE" + #LF$
vbs + ~"Set IE = CreateObject(\"InternetExplorer.Application\")" + #LF$
vbs + ~"IE.Navigate \"www.purebasic.com\"" + #LF$
vbs + ~"IE.Visible = True 'or hide" + #LF$
vbs + ~"Do While IE.busy = True" + #LF$
vbs + ~"  Runtime.Sleep 500" + #LF$
vbs + ~"Loop" + #LF$
vbs + ~"Runtime.String(\"result\") = IE.document.documentElement.outerHTML" + #LF$
vbs + ~"IE.Quit" + #LF$


If NewActiveScript()
  Debug "************************************************************"
  Debug vbs
  Debug "************************************************************"
  ParseScriptText(vbs)
  FreeActiveScript()
  Debug "************************************************************"
  Debug result
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Christophe_fr
User
User
Posts: 13
Joined: Thu Dec 13, 2018 9:58 pm
Location: france

Re: how to retrieve the HTML code from IE

Post by Christophe_fr »

Thank you mk-soft, but what I want is to have the html code when Internet Explorer is already launched. What I want is to retrieve information on a web page for my job (name, delivery date ...)

sorry for my english, I'm french
Sorry for my English. My language is French
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: how to retrieve the HTML code from IE

Post by mk-soft »

Ok...
I found this VB-Script with small bug

Code: Select all

Dim w, winTitle, oWindow

winTitle = "Your chosen title"
  for each w in createobject("shell.application").windows
    if instr(1, typename(w.document),"htmldocument", vbTextCompare) > 0 then
      if instr(1, w.document.title, sWinTitle, vbTextCompare) = 1 then 
        set oWindow = w
      end if
    end if
  next
  if not IsObject(oWindow) then 
    set oWindow = CreateObject("InternetExplorer.Application")
    oWindow.Navigate "about:Blank"
    do until oWindow.Readystate = 4 : wsh.sleep 50 : Loop
    oWindow.document.title = winTitle
  end if
PB

Code: Select all

;-TOP

; Comment   : Modul ActiveScript Example 7

; Link to ActiveScript  : https://www.purebasic.fr/english/viewtopic.php?f=12&t=71399
; Link to SmartTags     : https://www.purebasic.fr/english/viewtopic.php?f=12&t=71399#p527089
; Link to VariantHelper : https://www.purebasic.fr/english/viewtopic.php?f=12&t=71399#p527090

; ***************************************************************************************

XIncludeFile "Modul_ActiveScript.pb"
;XIncludeFile "Modul_SmartTags.pb"
;XIncludeFile "VariantHelper.pb"

UseModule ActiveScript
;UseModule ActiveSmartTags

; -------------------------------------------------------------------------------------

Global vbs.s, result.s
Global title.s
; Variable als Rumtime definieren zum Zugriff aus VB-Script
Runtime result
Runtime title

Title = "Purebasic"

; Daten anlegen

; VB-Script schreiben
vbs + #LF$ + ~" Dim w, sWinTitle, oShell, oWindow"
vbs + #LF$ + ~" "
vbs + #LF$ + ~" Set oShell = CreateObject(\"shell.application\")"
vbs + #LF$ + ~" "
vbs + #LF$ + ~" sWinTitle = Runtime.String(\"Title\")"
vbs + #LF$ + ~" "
vbs + #LF$ + ~" For Each w in oShell.Windows"
vbs + #LF$ + ~"   If InStr(1, typename(w.document),\"htmldocument\", 1) > 0 Then"
vbs + #LF$ + ~"     If InStr(1, w.document.title, sWinTitle, 1) = 1 Then "
vbs + #LF$ + ~"       Set oWindow = w"
vbs + #LF$ + ~"       Runtime.String(\"Title\") = w.document.title"
vbs + #LF$ + ~"     End If"
vbs + #LF$ + ~"   End If"
vbs + #LF$ + ~" Next"
vbs + #LF$ + ~" "
vbs + #LF$ + ~" Set oShell = Nothing"
vbs + #LF$ + ~" "
vbs + #LF$ + ~" If Not IsObject(oWindow) then "
vbs + #LF$ + ~"   Runtime.String(\"result\") = \"InternetExplorer: Page not found\""
vbs + #LF$ + ~" Else"
vbs + #LF$ + ~"   Runtime.String(\"result\") = oWindow.document.documentElement.outerHTML"
vbs + #LF$ + ~"   Set oWindow = Nothing"
vbs + #LF$ + ~" End If"
vbs + #LF$ + ~" "

If NewActiveScript()
  Debug "************************************************************"
  Debug vbs
  Debug "************************************************************"
  ParseScriptText(vbs);, #SCRIPTTEXT_ISVISIBLE)
  FreeActiveScript()
  Debug "************************************************************"
  Debug title
  Debug result
EndIf
Small update...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Christophe_fr
User
User
Posts: 13
Joined: Thu Dec 13, 2018 9:58 pm
Location: france

Re: how to retrieve the HTML code from IE

Post by Christophe_fr »

hello mk-soft,
it worked once but without giving me the code and since I have this error
ActiveScriptSite - OnScriptError
Line: 8 Column: 3
ActiveScriptSite - AddRef() : Refcount 1 > 2
ActiveScriptSite - QueryInterface() : IActiveScriptSiteInterruptPoll -> No Interface
ActiveScriptSite - AddRef() : Refcount 2 > 3
ActiveScriptSite - QueryInterface() : IActiveScriptSiteDebug -> No Interface
ActiveScriptSite - OnStateChange() : State = 5
ActiveScriptSite - OnStateChange() : State = 1
ActiveScriptSite - OnStateChange() : State = 2
************************************************************

Dim w, sWinTitle, oShell, oWindow

Set oShell = CreateObject("shell.application")

sWinTitle = Runtime.String("Title")

For Each w in oShell.Windows
If InStr(1, typename(w.document),"htmldocument", 1) > 0 Then
If InStr(1, w.document.title, sWinTitle, 1) = 1 Then
Set oWindow = w
Runtime.String("Title") = w.document.title
End If
End If
Next

Set oShell = Nothing

If Not IsObject(oWindow) then
Runtime.String("result") = "InternetExplorer: Page not found"
Else
Runtime.String("result") = oWindow.document.documentElement.outerHTML
Set oWindow = Nothing
End If

************************************************************
ActiveScriptSite - AddRef() : Refcount 3 > 4
ActiveScriptSite - OnEnterScript()
ActiveScriptSite - GetItemInfo() : VbsName = Runtime
Runtime - QueryInterface() : IDispatch -> Ok
Runtime - Release() : Refcount = 2 > 1
Runtime - AddRef() : Refcount = 1 > 2
Runtime - Release() : Refcount = 2 > 1
Runtime - GetIDsOfNames() : Name = string -> DispId = 3
Runtime - AddRef() : Refcount = 1 > 2
Runtime - Invoke() PropertyGetString : Title
Runtime - Release() : Refcount = 2 > 1
ActiveScriptSite - AddRef() : Refcount 4 > 5
ActiveScriptSite - QueryInterface() : IID {C5598E60-B307-11D1-B27D-006008C3FBFB} -> No Interface
ActiveScriptSite - QueryInterface() : IID {6D5140C1-7436-11CE-8034-00AA006009FA} -> No Interface
ActiveScriptSite - Release() : Refcount = 5 > 4
ActiveScriptSite - OnScriptError()
ActiveScriptSite - OnLeaveScript()
ActiveScriptSite - Release() : Refcount = 4 > 3
ActiveScriptSite - Release() : Refcount = 3 > 2
ActiveScriptSite - Release() : Refcount = 2 > 1
ActiveScriptSite - Release() : Refcount = 1 > 0
Runtime - Release() : Refcount = 1 > 0
************************************************************
PureBasic
sorry for my english, I'm french
Sorry for my English. My language is French
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: how to retrieve the HTML code from IE

Post by mk-soft »

Its work under Windows 7...

Perhaps Shell problem. Added On Error Resume Next.

Code: Select all

; VB-Script schreiben
vbs + #LF$ + ~" On Error Resume Next"
vbs + #LF$ + ~" "
vbs + #LF$ + ~" Dim w, sWinTitle, oShell, oWindow"
vbs + #LF$ + ~" "
vbs + #LF$ + ~" Set oShell = CreateObject(\"shell.application\")"
vbs + #LF$ + ~" "
vbs + #LF$ + ~" sWinTitle = Runtime.String(\"Title\")"
vbs + #LF$ + ~" "
vbs + #LF$ + ~" For Each w in oShell.Windows"
vbs + #LF$ + ~"   If InStr(1, typename(w.document),\"htmldocument\", 1) > 0 Then"
vbs + #LF$ + ~"     If InStr(1, w.document.title, sWinTitle, 1) = 1 Then "
vbs + #LF$ + ~"       Set oWindow = w"
vbs + #LF$ + ~"       Runtime.String(\"Title\") = w.document.title"
vbs + #LF$ + ~"     End If"
vbs + #LF$ + ~"   End If"
vbs + #LF$ + ~" Next"
vbs + #LF$ + ~" "
vbs + #LF$ + ~" Set oShell = Nothing"
vbs + #LF$ + ~" "
vbs + #LF$ + ~" If Not IsObject(oWindow) then "
vbs + #LF$ + ~"   Runtime.String(\"result\") = \"InternetExplorer: Page not found\""
vbs + #LF$ + ~" Else"
vbs + #LF$ + ~"   Runtime.String(\"result\") = oWindow.document.documentElement.outerHTML"
vbs + #LF$ + ~"   Set oWindow = Nothing"
vbs + #LF$ + ~" End If"
vbs + #LF$ + ~" "
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Christophe_fr
User
User
Posts: 13
Joined: Thu Dec 13, 2018 9:58 pm
Location: france

Re: how to retrieve the HTML code from IE

Post by Christophe_fr »

That's exactly what I wanted ! mk-soft you are the best :D
Sorry for my English. My language is French
Post Reply