CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: WebBrowser.Document.GetElementByID() problem

  1. #1
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Post WebBrowser.Document.GetElementByID() problem

    Hi, I'm trying to load an HTML page inside a webbrowser in my WinForms app and would like to set text values of several elements of the page in order to localize it according to language.

    The poblem is, I cannot get any elements from the Document...
    This is how I load my document, which is stored locally.
    Code:
    this.webBrowser.Navigate(new Uri(Application.StartupPath + @"\Help\" + TargetLanguage.GetString("Help_FileName")));
    And this is the HTML loaded (fragment)
    HTML Code:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=11" >
        
        <title>Help page</title>
        <style>
        div.main {
            margin-left: auto;
            margin-right: auto;
        }
        p.regular {
            margin-left: 3%;
            word-wrap: break-word;
            width: 900px;
            max-width: 95%;
        }
        </style>
    </head>
    <body bgcolor="#B6F2C5">
        <div>
            <div>
                <p name="TestID" id="TestID" class="regular">Hello world<p>
                ...
            </div>
        </div>
    </body>
    I've tried any combination of GetElementByID() and GetElementsByTagName() with "TestID", with "p" and "div" and I never get an element.
    I've looked it up on multiple forums and none of the suggested solutions work for me.

    ElementByID is always null
    ElementsByTag is always empty collection
    WebBrowser.Document.Body is always null (not sure if this means anything anyway)

    Neither getting elements by tag (p, div, etc) nor getting elements by ID/Name works...

    How can I get this done?

    And once I eventually get the element I'm looking for, will *.InnerText = "something"; work or how do I set its text then?
    Thank you for any help!

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,268

    Re: WebBrowser.Document.GetElementByID() problem

    HAve a look into the following:

    System.Net.WebClient
    HtmlElementCollection
    ocument.GetElementsByTagName

    You may also want to have a look at my two articles - Yes, I know that they are in VB, but you will still be able to get the whole idea of how this can be achieved :

    How to use Visual Studio 2012 to Download Images from Websites
    http://www.codeguru.com/columns/vb/h...m-websites.htm

    Creating a Web Text Scraper with Visual Basic
    http://www.codeguru.com/columns/vb/c...sual-basic.htm
    [SIGPIC][/SIGPIC]
    All my Articles
    Hannes

  3. #3
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Re: WebBrowser.Document.GetElementByID() problem

    Hi Hannes, thanks for the answer. I had already tried all those, that's why I mention that things like ElementByID and ElementsByTag return null and empty collection. I ended up coding an HTMLConstruction class that will take the help page structure and add texts from within my App, and then just set the WebBrowser.DocumentText instead of navigating to an URL. This way I just read whatever text tag I need from the target language resx file and add that to my HTML. After all it's just a help page with static texts... nothing fancy.

    Thanks anyway!

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)