简体   繁体   English

WebBrowser控件更改属性

[英]WebBrowser Control Changing Attributes

WebBrowser Control seems to re-arrange attributes within HTML tags when setting webBrowser1.DocumentText.. 设置webBrowser1.DocumentText。时, WebBrowser Control似乎在HTML标记中重新排列属性。

I'm wondering if there is some kind of render mode or Document Encoding that I am missing. 我想知道是否缺少某种渲染模式或文档编码。 My problem can be seen by simply adding a RichTextBoxControl (txt_htmlBody) and a WebBrowser control (webBrowser1) to a windows form. 只需将RichTextBoxControl (txt_htmlBody)和WebBrowser控件(webBrowser1)添加到Windows窗体中,就可以看到我的问题。

Add webBrowser1 WebBrowser Control, and add an event handler to; 添加webBrowser1 WebBrowser控件,并向其中添加事件处理程序; webBrowser1_DocumentCompleted

I used this to add my mouse click event to the web browser control. 我用它来将鼠标单击事件添加到Web浏览器控件中。

  private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        // Attach an event to handle mouse clicks on the web browser
        this.webBrowser1.Document.Body.MouseDown += new HtmlElementEventHandler(Body_MouseDown);
    }

In the mouse click event, we get which element was clicked on like so; 在鼠标单击事件中,我们像这样获取被单击的元素。

   private void Body_MouseDown(Object sender, HtmlElementEventArgs e)
    {
        // Get the clicked HTML element
        HtmlElement elem = webBrowser1.Document.GetElementFromPoint(e.ClientMousePosition);

        if (elem != null)
        {
            highLightElement(elem);

        }
    }

    private void highLightElement(HtmlElement elem)
    {

        int len = this.txt_htmlBody.TextLength;
        int index = 0;

        string textToSearch = this.txt_htmlBody.Text.ToLower(); // convert everything in the text box to lower so we know we dont have a case sensitive issues
        string textToFind = elem.OuterHtml.ToLower();
        int lastIndex = textToSearch.LastIndexOf(textToFind); 
        // We cant find the text, because webbrowser control has re-arranged attributes in the <img> tag
        // Whats rendered by web browser: "<img border=0 alt=\"\" src=\"images/promo-green2_01_04.jpg\" width=393 height=30>"
        // What was passed to web browser from textbox: <img src="images/PROMO-GREEN2_01_04.jpg" width="393" height="30" border="0" alt=""/>
        // As you can see, I will never be able to find my data in the source because the webBrowser has changed it

    }

Add txt_htmlBody RichTextBox to the form, and set a TextChanged of the RichTextBox event to set the WebBrowser1.DocumentText as the RichTextBox (txt_htmlBody) text changed. txt_htmlBody RichTextBox添加到窗体,并设置RichTextBox事件的TextChanged以将WebBrowser1.DocumentText设置为RichTextBox (txt_htmlBody)文本已更改。

   private void txt_htmlBody_TextChanged(object sender, EventArgs e)
    {
        try
        {

            webBrowser1.DocumentText = txt_htmlBody.Text.Replace("\n", String.Empty);

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

When you run your program, copy the below example HTML into txt_htmlBody, and click the Image on the right and debug highLightElement. 运行程序时,将以下示例HTML复制到txt_htmlBody中,然后单击右侧的Image并调试highLightElement。 You will see by my coments why I can not find the specified text in my search string, because WebBrowser control re-arranges the attributes. 通过我的评论,您将看到为什么无法在搜索字符串中找到指定的文本,因为WebBrowser控件会重新排列属性。

<img src="images/PROMO-GREEN2_01_04.jpg" width="393" height="30" border="0" alt=""/>

Does anyone know how to make WebBrowser control render my HTML as-is? 有谁知道如何使WebBrowser控件按原样呈现我的HTML?

Thank you for your time. 感谢您的时间。

You cannot expect the processed HTML to be 1:1 the same as the original source, when you obtain it back via element.OuterHtml . 当您通过element.OuterHtml获得处理后的HTML时,您不能期望它与原始源的1:1相同。 It's almost never the same, regardless of the rendering mode. 不论渲染模式如何,它几乎永远不会相同。

However, despite the attributes may have got rearranged, their names and values are still the same, so you'd just need to improve your search logic (eg, by walking the DOM three or simply enumerating elements via HtmlDocument.All and checking their attributes via HtmlElement.GetAttribute ). 但是,尽管属性可能已经重新排列,但是它们的名称和值仍然相同,因此您只需要改善搜索逻辑(例如,通过遍历DOM三种或通过HtmlDocument.All枚举元素并检查其属性)即可。通过HtmlElement.GetAttribute )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM