2013-02-08 49 views
1

當我實例化一個IE對象並導航到一個url時,我不知道如何從該地址獲取源HTML代碼。如何從InternetExplorer對象查看源代碼HTML?

這是我使用的代碼:

SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer(); 
IE.Visible = false; 
IE.Navigate("www.testsite.com"); 

我想是這樣的:

string source = IE.ToSource(); 

所以我可以檢查它的內容。 我可以做到這一點嗎?謝謝。

回答

4

試一下:

SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer(); 
IE.Visible = false; 
IE.Navigate("www.testsite.com"); 
mshtml.IHTMLDocument2 htmlDoc 
     = IE.Document as mshtml.IHTMLDocument2; 
string content = htmlDoc.body.outerHTML; 

您可以從body.parent屬性訪問整個HTML字符串:

string content = htmlDoc.body.parent.outerHTML; 

You can see a nice example here (the example in c++)