1
我有下面的代码,它只是简单地从网站上获取文档文本,但我无法让它工作。看起来文档完成方法(PrintDocument)永远不会被调用。控制台刚刚打开并坐在那里,没有任何东西印在屏幕上。从网页浏览器获取文档文本
class BrowserControl
{
public void PrintHelpPage()
{
WebBrowser webBrowserForPrinting = new WebBrowser();
webBrowserForPrinting.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(PrintDocument);
webBrowserForPrinting.Url = new Uri("http://www.fooweb.com");
}
public void PrintDocument(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
// Print the document now that it is fully loaded.
string test = ((WebBrowser)sender).DocumentText;
Console.WriteLine(test);
Console.WriteLine("DONE! ---------------------");
}
}
class Program
{
public static void Main(string[] args)
{
BrowserControl browser = new BrowserControl();
Thread browserThread = new Thread(browser.PrintHelpPage);
browserThread.SetApartmentState(ApartmentState.STA);
browserThread.Start();
Console.ReadKey();
}
}
很好。这很有帮助。但是,运行此时我收到以下错误信息:无法获取'WebBrowser'控件的窗口句柄。不支持Windowless ActiveX控件。'从阅读其他答案,似乎我需要调用控件? – awwwyissss
您可以在没有窗口的情况下使用WebBrowser控件。我在[triflejs.org](http://triflejs.org/)上使用它作为无头IE浏览器。我想你只是错过了对'browser.PrintHelpPage()'的调用 –