使用 COM 对象实现浏览器自动化 .net

Using COM objects for browser automation .net

阅读一些关于 COM 接口的 wiki。浏览论坛并尝试尽我所能,似乎在 .NET 中开发浏览器自动化控制台应用程序时,许多人不赞成使用它们。

有什么可以替代我常用的

    Dim ie As InternetExplorer

        ie = New InternetExplorer
        ie.Visible = True
        ie.Navigate(website)

我不确定是否允许使用最佳实践类型的问题,但我非常想知道这个问题的答案。主要是替代品,然后当然是短的,为什么?谢谢你们:)!

事实上,您可以在多线程+console/winforms应用程序中使用 Webbrowser 控件。

基于这个答案:Run and control browser control in different thread

var html = RunWBControl("http://google.com").Result;

static public Task<string> RunWBControl(string url)
{
    var tcs = new TaskCompletionSource<string>();
    var th = new Thread(() =>
    {
        WebBrowserDocumentCompletedEventHandler completed = null;

        using (WebBrowser wb = new WebBrowser())
        {
            completed = (sndr, e) =>
            {
                tcs.TrySetResult(wb.DocumentText);
                wb.DocumentCompleted -= completed;
                Application.ExitThread();
            };

            wb.DocumentCompleted += completed;
            wb.Navigate(url);
            Application.Run();
        }
    });

    th.SetApartmentState(ApartmentState.STA);
    th.Start();

    return tcs.Task;
}