Results 1 to 19 of 19

Thread: [RESOLVED] WebBrowser control - get source code of HTML page?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    66

    Resolved [RESOLVED] WebBrowser control - get source code of HTML page?

    Hello,

    I'm writing a small app, but I have a problem. The app has a WebBrowser control (AxWebBrowser). I'm using the Naviagate2 method to make the control show a web page. How can I get the source code of the viewed page?
    "Imagination is more important than knowledge!" Albert Einstein

    If you like this answer/question, please rate it! Thank you!

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: WebBrowser control - get source code of HTML page?

    load the page into an HTML object, then use the .documentElement.innerHTML method..

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    66

    Re: WebBrowser control - get source code of HTML page?

    How is this supposed to be done?
    "Imagination is more important than knowledge!" Albert Einstein

    If you like this answer/question, please rate it! Thank you!

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: WebBrowser control - get source code of HTML page?

    hmm I've done a program that was pulling the innertext (innerhtml is just another method that is similar) from an Internet Explorer window using Window Handles... not by using the AxWebBrowser... if you want i can see about cleanin up the code a little and posting...

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    66

    Re: WebBrowser control - get source code of HTML page?

    I need to use a AnWebBrowser control
    "Imagination is more important than knowledge!" Albert Einstein

    If you like this answer/question, please rate it! Thank you!

  6. #6
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: WebBrowser control - get source code of HTML page?

    Actually.. it's going to be overkill for what you need because its using API calls.. I'm assuming your program already has the HTML document being displayed, so just run that object's method...

    VB Code:
    1. Dim myHTMLDoc as HTMLDocument = (your html document)
    2. Dim strHTML As String
    3. strHTML = myHTMLDoc.documentElement.innerHTML.ToString

    This requires a reference to mshtml, and the Imports mshtml at the top...
    Last edited by gigemboy; Sep 27th, 2005 at 02:42 PM.

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: WebBrowser control - get source code of HTML page?

    you need to set a reference to MSHTML, this will give you the objects you need to access the HTML Source of a page.

    AxWebBrowser.Document IS an htmldocument, but its represented as an OBJECT, because the webbrowser is being imported from COM.

    So you need to do something like

    VB Code:
    1. Dim MyDoc As mshtml.HTMLDocument
    2.                 MyDoc = CType(wb.Document, mshtml.HTMLDocument)
    3.                 Debug.Write(MyDoc.documentElement.innerHTML)
    where wb is the webbrowser control

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: WebBrowser control - get source code of HTML page?

    except yours won't work with option strict on..

    but thank you for deleting your post.. as it wasn't really necessary.. but don't worry.. i don't take offense

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    66

    Re: WebBrowser control - get source code of HTML page?

    OK kleinma, your suggestion worked but another problem poped out - it gets only what's between the <head> tags
    "Imagination is more important than knowledge!" Albert Einstein

    If you like this answer/question, please rate it! Thank you!

  10. #10
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: WebBrowser control - get source code of HTML page?

    Use outerHTML....

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    66

    Re: WebBrowser control - get source code of HTML page?

    It's the same - only what's between <head>
    "Imagination is more important than knowledge!" Albert Einstein

    If you like this answer/question, please rate it! Thank you!

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    66

    Re: WebBrowser control - get source code of HTML page?

    In fact it gets only the code before <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> and puts <head> tags around this code
    "Imagination is more important than knowledge!" Albert Einstein

    If you like this answer/question, please rate it! Thank you!

  13. #13
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: WebBrowser control - get source code of HTML page?

    Hmm I think something else is happening then... it doesn't "put" anything.. it just reports back the text... it showed head tags there because it found head tags there... sure you are referenceing the right object and nothing else is going on???

  14. #14
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: WebBrowser control - get source code of HTML page?

    try another HTML document to make sure its not just the current HTML you are working with.. perhaps it is not formed correctly or something like that?

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    66

    Re: WebBrowser control - get source code of HTML page?

    Oh, sorry my mistake - the page I was loading refreshed and loaded another page. I had to call this after the second page is loaded - now it worked Thanks for the help guys...
    "Imagination is more important than knowledge!" Albert Einstein

    If you like this answer/question, please rate it! Thank you!

  16. #16
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: WebBrowser control - get source code of HTML page?

    also make sure you put this in the DocumentComplete event of the browser... if you put it on the navigatecomplete event, the page may not be fully loaded yet.

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    66

    Re: WebBrowser control - get source code of HTML page?

    That's the mistake, in fact
    "Imagination is more important than knowledge!" Albert Einstein

    If you like this answer/question, please rate it! Thank you!

  18. #18
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [RESOLVED] WebBrowser control - get source code of HTML page?

    lol.. I had a feeling it was

  19. #19
    Lively Member
    Join Date
    Jan 2000
    Location
    Thessaloniki ,Greece
    Posts
    100

    Re: [RESOLVED] WebBrowser control - get source code of HTML page?

    How can I put this html source code back to the browser web page?

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width