How to grab the Contents Which updated by JavaScript WebBrowser

跟風遠走 提交于 2019-12-23 01:16:22

问题


private void button1_Click(object sender, EventArgs e)
{
    webBrowser1.Navigate(textBox1.Text);
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    WebBrowser web = (WebBrowser)sender;
    richTextBox1.Text = web.DocumentText;
}

above is sample code. it's giving all Text of Current Open, if contents is updated by JavaScript, it visible but Document.Text not update.

Please Help guys


回答1:


I had the same problem. Use the following sample code:

IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
string content = doc.body.innerText;

Also, add mshtml to the references of your project (if you dont know how to add the refernce, just google it).

Actually, whenever you use this code, the value in the doc variable is the updated version of the contents of the webbrowser.

Good Luck




回答2:


I would guess that the javascript that is executing on the page which is modifying the content is happening after the DocumentCompleted event; Perhaps you can try a different event such as 'Invalidated'.

The WebBrowser.DocumentText also many not reflect any changes to the DOM, and you may need to navigate the DOM through the WebBrowser.Document property.

http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.document.aspx



来源:https://stackoverflow.com/questions/7349667/how-to-grab-the-contents-which-updated-by-javascript-webbrowser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!