C#: 如何从WebBrowser元素中获取文档标题?

5

我在尝试从C#的WebBrowser中获取文档标题时遇到了问题。在VB.NET中可以正常工作,但在C#中不会给我任何属性。

当我键入MyBrowser.Document.时,我只得到4个方法的选项:Equals、GetHashCode、GetType和ToString - 没有属性。

我认为这是因为我必须先将文档分配给一个新实例,但我找不到在VB.NET中存在的HTMLDocument类。

基本上我想要做的是每次WebBrowser加载/重新加载页面时返回Document.Title。

有人能帮忙吗?非常感谢!

这是我目前拥有的代码...

private void Link_Click(object sender, RoutedEventArgs e)
{
    WebBrowser tempBrowser = new WebBrowser();
    tempBrowser.HorizontalAlignment = HorizontalAlignment.Left;
    tempBrowser.Margin = new Thickness(-4, -4, -4, -4);
    tempBrowser.Name = "MyBrowser";
    tempBrowser.VerticalAlignment = VerticalAlignment.Top;
    tempBrowser.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(tempBrowser_LoadCompleted);

    tempTab.Content = tempBrowser; // this is just a TabControl that contains the WebBrowser

    Uri tempURI = new Uri("http://www.google.com");
    tempBrowser.Navigate(tempURI);
}

private void tempBrowser_LoadCompleted(object sender, EventArgs e)
{
    if (sender is WebBrowser)
    {
        MessageBox.Show("Test");
        currentBrowser = (WebBrowser)sender;
        System.Windows.Forms.HtmlDocument tempDoc = (System.Windows.Forms.HtmlDocument)currentBrowser.Document;
        MessageBox.Show(tempDoc.Title);
    }
}

这段代码没有任何错误,但我从未看到第二个消息框。尽管如此,我确实看到了第一个消息框(即“测试”消息),因此程序已经执行了该代码块。

你需要对HTMLDocument进行强制类型转换。 - Mau
5个回答

4

添加对Microsoft.mshtml的引用

为LoadCompleted添加事件接收器

webbrowser.LoadCompleted += new LoadCompletedEventHandler(webbrowser_LoadCompleted);

那么您在读取数值时就不会遇到文档未按顺序加载而无法读取的问题。
    void webbrowser_LoadCompleted(object sender, NavigationEventArgs e)
    {
        // Get the document title and display it
        if (webbrowser.Document != null)
        {
            mshtml.IHTMLDocument2 doc = webbrowser.Document as mshtml.IHTMLDocument2;
            Informative.Text = doc.title;
        }
    }

2
您没有使用Windows Forms WebBrowser控件。我认为您获得了ieframe.dll的COM包装器,其名称为AxWebBrowser。通过在“解决方案资源管理器”窗口中打开“引用”节点来验证。如果您看到AxSHDocVw,则获取了错误的控件。它非常不友好,只会为文档属性提供不透明的接口指针。您确实只能获得默认对象类成员。
在工具箱中查找。选择WebBrowser而不是“Microsoft Web Browser”。

Hans,解决方案资源管理器显示SHDocVw。我的应用程序是WPF的,我只是在其中使用了默认的WebBrowser控件。我需要添加“using System.Windows.Forms”引用并使用那个WebBrowser吗? - Randy Cleary
好的,就这样了。请查看:http://social.msdn.microsoft.com/forums/en-US/wpf/thread/e60b671e-84e6-40a4-a37a-e0a8610aef44 - Hans Passant
谢谢,我会查看链接并看看我能做什么。然而,我发现有点奇怪的是,我已经在我的当前代码上工作了一段时间,没有出现任何错误,但我也没有得到它尝试做的东西。你能否查看我编辑过的帖子中的代码,看看是否有任何问题?可能只是不可能,但我认为如果是这种情况,它会给我一个错误消息。 - Randy Cleary
那段代码不能工作,不确定为什么你的程序没有因为 InvalidCast 异常而崩溃。 - Hans Passant
好的,我按照您的指示使用了WindowsFormsHost,现在它运行得非常好!非常感谢您! - Randy Cleary

0

最终与以下内容良好配合:

using System.Windows.Forms;

...

WebBrowser CtrlWebBrowser = new WebBrowser();

...

CtrlWebBrowser.Document.Title = "Hello World";
MessageBox.Show( CtrlWebBrowser.Document.Title );

0

LoadCompleted事件不会触发。你应该使用Navigated事件处理程序代替它。

webBrowser.Navigated += new NavigatedEventHandler(WebBrowser_Navigated);

(...)

private void WebBrowser_Navigated(object sender, NavigationEventArgs e)
{
        HTMLDocument doc = ((WebBrowser)sender).Document as HTMLDocument;

        foreach (IHTMLElement elem in doc.all)
        {
            (...)
        }
        // you may have to dispose WebBrowser object on exit
}

0
string title = ((HTMLDocument)MyBrowser.Document).Title

或者

HTMLDocument Doc =  (HTMLDocument)MyBrowser.Document.Title ;
string title = doc.Title;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接