C# 创建一个使用WebBrowser并返回HTML的方法?

C# 创建一个使用WebBrowser并返回HTML的方法?,c#,.net,webbrowser-control,C#,.net,Webbrowser Control,如何创建执行此操作的方法: 登录到一个网站,然后阅读会员专页并返回HTML 我提出了这个显然不起作用的方法,因为我不知道如何让它返回页面内容 public string LoginAndReadPage() { WebBrowser wb = new WebBrowser(); wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted); wb.Navi

如何创建执行此操作的方法: 登录到一个网站,然后阅读会员专页并返回HTML

我提出了这个显然不起作用的方法,因为我不知道如何让它返回页面内容

public string LoginAndReadPage() {
    WebBrowser wb = new WebBrowser();
    wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
    wb.Navigate("hxxp://mysite.com/login");
}

private async void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (wb.Url.ToString().Contains("login"))
    {
        wb.Document.GetElementsByTagName("input").GetElementsByName("email")[0].SetAttribute("value", _login);
        wb.Document.GetElementsByTagName("input").GetElementsByName("password")[0].SetAttribute("value", _password);
        wb.Document.GetElementsByTagName("button")[0].InvokeMember("click");
    }
    else if (wb.Url.ToString().Contains("dashboard"))
    {
        return wb.DocumentText; // I want to return the content of mysite.com/dashboard
    }
    else
    {
        await Task.Delay(1000); //wait for 1 second just to let the WB catch up
        wb.Navigate("hxxp://mysite.com/dashboard");
    }
}

提前感谢

你要做的就是所谓的“刮网”,有时是“拉网”。这是一个大话题,所以我推荐


可能您也可以使用类似于通过C驱动程序的方法来实现这一点。Selenium是为自动化用户界面测试而设计的,但它绝对拥有您所需的所有工具。

wb.Document是DOM,您还需要什么?@Edmad:我想返回它,可能是通过LoginAndReadPage方法。您必须使用web浏览器控件吗?@Niklas否,您有其他解决方案吗?该网页使用AngularJS加载内容,而OAuthIm对此并不熟悉,不过请检查一下这是否有效?