Yustme
Yustme

Reputation: 6265

get document.text from webbrowsercontrol

How do i get the document.text from the webbrowser control in WPF?

This control doesn't have this property anymore in WPF. I have looked in the msdn articles and there is nothing about it.

Also, googling this up didn't give me any results on this control in WPF

Upvotes: 1

Views: 3743

Answers (1)

Gayot Fow
Gayot Fow

Reputation: 8792

After the browser has finished navigating, you can call a method like this...

public static void GetPageContent(WebBrowser wb, string fileName)
{
    mshtml.HTMLDocumentClass dom = (mshtml.HTMLDocumentClass)wb.Document;
    Task t = new Task(() => File.WriteAllText(fileName, dom.body.innerHTML));
    t.Start();
}

This method places the entire page into a standard ASCII file and you can parse out content according to your need.

To use this method, you will need to include a reference to the Microsoft.mshtml assembly, which should be located in the .NET tab of the 'Add Reference' dialog. As shown, I spawn off a separate thread in order to keep the UI perky, but this is optional.

For the case where you simply want the string returned to the client, you can use this method...

public static string GetPageContent(WebBrowser wb)
{
    return ((mshtml.HTMLDocumentClass) wb.Document).body.innerHTML;
}

Upvotes: 3

Related Questions