我需要在頁面上執行javascript後訪問HTML文檔的DOM。我有下面的代碼連接到URL並獲取文檔。 的問題是,它從來沒有得到過DOM修改使用javascript後使用WebBrowser訪問DOM
public class CustomBrowser
{
public CustomBrowser()
{
//
// TODO: Add constructor logic here
//
}
protected string _url;
string html = "";
WebBrowser browser;
public string GetWebpage(string url)
{
_url = url;
// WebBrowser is an ActiveX control that must be run in a
// single-threaded apartment so create a thread to create the
// control and generate the thumbnail
Thread thread = new Thread(new ThreadStart(GetWebPageWorker));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
string s = html;
return s;
}
protected void GetWebPageWorker()
{
browser = new WebBrowser();
// browser.ClientSize = new Size(_width, _height);
browser.ScrollBarsEnabled = false;
browser.ScriptErrorsSuppressed = true;
//browser.DocumentCompleted += browser_DocumentCompleted;
browser.Navigate(_url);
// Wait for control to load page
while (browser.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
Thread.Sleep(5000);
var documentAsIHtmlDocument3 = (mshtml.IHTMLDocument3)browser.Document.DomDocument;
html = documentAsIHtmlDocument3.documentElement.outerHTML;
browser.Dispose();
}
}
我希望有人能幫助我解決這個問題
請不要將代碼作爲圖像發佈。以文本形式發佈代碼。此外,您應該使用事件來查找導航何時完成,而不是使用Application.DoEvents()或Thread.Sleep()的'while循環。 –
我添加了代碼作爲文本,圖像來澄清瀏覽器中的dom和我得到的東西之間的區別 –
如何使用替代控件?例如。 http://stackoverflow.com/questions/790542/replacing-net-webbrowser-control-with-a-better-browser-like-chrome – user1946932