如何获取WebBrowser控件中的页面标题?

发布于 2024-12-08 14:16:39 字数 401 浏览 1 评论 0原文

当我导航到不同的网站时,如何获取 WebBrowser 控件中的页面标题?


开头的xmlns属性

xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"

以 D

DataContext
DesiredSize
Dispatcher
DoubleTap

xaml 标记

<phone:WebBrowser Name="browser" Height="760" VerticalAlignment="Top"></phone:WebBrowser>

How can I get the page title in a WebBrowser control when I navigate to different websites?


xmlns

xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"

Properties starting with D

DataContext
DesiredSize
Dispatcher
DoubleTap

xaml tag

<phone:WebBrowser Name="browser" Height="760" VerticalAlignment="Top"></phone:WebBrowser>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

灯角 2024-12-15 14:16:39

我也有同样的问题。 @Akash Kava 的答案几乎是正确的,但这是读取 html 标题的正确 javascript:

String title = (string)browser.InvokeScript("eval", "document.title.toString()");

I had the same problem. @Akash Kava's answer is almost correct but this is the correct javascript to read the html title:

String title = (string)browser.InvokeScript("eval", "document.title.toString()");
離人涙 2024-12-15 14:16:39

对我来说,以下代码有效。 @Akash 和 @Mikko 的答案让我走上了正确的道路,但我在一些网站上仍然遇到了一些问题。据我了解,问题是当 WebBrowser 组件开始从远程服务器获取数据时,会引发 Navigated 事件。因此 DOM 对象尚未完成,因此调用 document.title 会引发错误。所以我只是在几毫秒后重试,直到获得标题。在我测试的任何网站上,这个“循环”从未迭代超过 3 次,并且每次都完美地给我带来了标题。

private void webBrowser1_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
    ThreadPool.QueueUserWorkItem(UpdateText);
}

private void UpdateText(object o)
{
    Thread.Sleep(100);
    Dispatcher.BeginInvoke(() =>
    {
        try
        {
            textBlock1.Text = webBrowser1.InvokeScript("eval", "document.title").ToString();
        }
        catch (SystemException)
        {
            ThreadPool.QueueUserWorkItem(UpdateText);
        }
    });
}

For me the following code works. The answers from @Akash and @Mikko set me on the right path, but I still had some problems with a few websites. The problem as I understand it is that the Navigated event is raised when the WebBrowser component starts getting data from the remote server. As such the DOM object is not yet complete, so calling the document.title throws an error. So I just retry after a few milliseconds till I get the title. This "loop" has never iterated more than 3 times on any website I tested and flawlessly brought me the title every time.

private void webBrowser1_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
    ThreadPool.QueueUserWorkItem(UpdateText);
}

private void UpdateText(object o)
{
    Thread.Sleep(100);
    Dispatcher.BeginInvoke(() =>
    {
        try
        {
            textBlock1.Text = webBrowser1.InvokeScript("eval", "document.title").ToString();
        }
        catch (SystemException)
        {
            ThreadPool.QueueUserWorkItem(UpdateText);
        }
    });
}
尤怨 2024-12-15 14:16:39

我很确定这

String title = browser.Document.Title;

应该可以解决问题。

请参阅此处

I'm pretty sure that

String title = browser.Document.Title;

should do the trick.

See here.

秋凉 2024-12-15 14:16:39

所有答案并非 100% 正确:

您必须调用以下内容:

String title = (string)browser.InvokeScript("eval", "document.title.toString()");

在浏览器的 LoadCompleted 事件中,而不是在 navigated 事件中。

All answers are not 100% correct:

You must call the following:

String title = (string)browser.InvokeScript("eval", "document.title.toString()");

in the LoadCompleted event of the browser, and not in the navigated event.

月依秋水 2024-12-15 14:16:39

您可以使用 InvokeScript 获取标题因为,

 String title = browser.InvokeScript("document.title");

我不知道它是否正确,但你也可以尝试 window.title 。

You can use InvokeScript to get title as,

 String title = browser.InvokeScript("document.title");

I don't know it's correct or not but you can try window.title too.

花心好男孩 2024-12-15 14:16:39

下面的代码对我有用,请注意导航事件,如果您使用已加载,它将在页面加载之前触发,您希望它在页面完全加载“之后”的某个时间触发,导航充当该事件。

private void web1_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        //Added thread using "using System.Thread", to act as a buffer for any page delay.
        Thread.Sleep(2000);
        String title = (string)web1.InvokeScript("eval", "document.title");
        PageTitle.Text = title;

    }

The code below works for me, note the navigated event, if you use loaded it will trigger just before the page is loaded, you want that to trigger sometime "after" the page is Fully Loaded, navigated acts as that event.

private void web1_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        //Added thread using "using System.Thread", to act as a buffer for any page delay.
        Thread.Sleep(2000);
        String title = (string)web1.InvokeScript("eval", "document.title");
        PageTitle.Text = title;

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文