Respondida How to load part of DOM in web browser control.

  • sábado, 04 de agosto de 2012 15:31
     
     

    Hi all,

    I have a html page and wanted to load on web browser control. I am able to display full html page on control but I don't want to show all the page content on web browser control. What I want to show is, DOM has div control and I want to show only particular DIV on web browser control.

    Please help me to resolve this issue.

    Thanks

Todas las respuestas

  • sábado, 04 de agosto de 2012 16:57
    Moderador
     
     Respondida Tiene código

    When I was doing webpage form automation stuff, I found HTMLDocument2 to be the best to use.
    You can get to forms, divs, any part you need. Read up on that and search for snippets that will help.
    With this method you can literally rewrite the page or remove parts of it.

    Below is a dump of the section from one of my projects that finds the form (usually the first), then two methods for writing to textbox and textarea fields.

    Also included is the code to stop javascript alerts, if it helps :)

    This should at least point you in the route I'd take.

            private bool UpdateTextInput(int formId, string name, string text, bool canSubmit)
            {
                bool successful = false;
                IHTMLFormElement form = GetForm(formId);
                if (form != null)
                {
                    var element = form.item(name: name);
                    if (element != null)
                    {
                        var textarea = element as HTMLInputElement;
                        textarea.value = text;
                        successful = true;
                    }
    
                    if (successful && canSubmit)
                        form.submit();
                }
    
                return successful;
            }
    
            bool UpdateTextArea(int formId, string name, string text, bool canSubmit)
            {
                bool successful = false;
                IHTMLFormElement form = GetForm(formId);
                if (form != null)
                {
                    var element = form.item(name: name);
                    if (element != null)
                    {
                        var textarea = element as HTMLTextAreaElement;
                        textarea.value = text;
                        successful = true;
                    }
    
                    if (successful && canSubmit)
                        form.submit();
                }
    
                return successful;
            }
    
            // --------------------------------------------------------------------------------------------------------------------------------------
    
            private IHTMLFormElement GetForm(int formNo)
            {
                IHTMLDocument2 doc = (IHTMLDocument2)bws.Document;
                IHTMLElementCollection forms = doc.forms;
                var ix = 0;
                foreach (IHTMLFormElement f in forms)
                    if (ix++ == formNo)
                        return f;
    
                return null;
            }
    
    
            public void HideScriptErrors(WebBrowser wb, bool Hide)
            {
                FieldInfo fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
                if (fiComWebBrowser == null) return;
                object objComWebBrowser = fiComWebBrowser.GetValue(wb);
                if (objComWebBrowser == null) return;
                objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { Hide });
            }
     
    Regards,
    Pete

    #PEJL