12

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>
BrunoLM
  • 97,872
  • 84
  • 296
  • 452

6 Answers6

9

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()");
Mikko Lassila
  • 116
  • 1
  • 3
  • As David said, to work properly this must be called in the **WebBrowser.LoadCompleted** event handler. In addition, since this is being executed in an event handler, the above code should be called within the action of either `Control.Invoke` for WPF or `Dispatcher.Invoke` for Silverlight/Windows Phone if the UI is being updated. – Ben Feb 15 '14 at 03:45
2

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);
        }
    });
}
tasosval
  • 21
  • 1
  • 3
1

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.

David
  • 3,971
  • 1
  • 26
  • 65
1

I'm pretty sure that

String title = browser.Document.Title;

should do the trick.

See here.

Sid Holland
  • 2,871
  • 3
  • 27
  • 43
  • Properties starting with D: DataContext, DesiredSize, Dispatcher, DoubleTap. (Microsoft.Phone.Controls). xaml tag: `` – BrunoLM Oct 08 '11 at 21:41
  • 1
    Sorry, was thinking of the regular Forms WebBrowser control. Have you tried @Akash Kava's answer but with the `IsScriptEnabled` property set to `true`? – Sid Holland Oct 08 '11 at 21:58
0

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;

    }
Chad Adams
  • 1,319
  • 1
  • 9
  • 14
0

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.

Akash Kava
  • 39,066
  • 20
  • 121
  • 167
  • I tried on `LoadCompleted` event and it thrown an unknown exception. – BrunoLM Oct 08 '11 at 21:41
  • You must enable scripting as per msdn documentation and also this method can only be called after document was loaded so you must wait for sometime if document is not ready. – Akash Kava Oct 09 '11 at 05:30
  • 1
    I tried calling this when receiving the Navigated event and get an exception that reads "Error: 80020006". And yes, I have IsScriptEnabled="True" in my XAML. – tronman Nov 14 '11 at 18:11