How to read element content from a web page using the WPF WebBrowser component

How to get element value from a web page in C # using WPF WebBrowser component?

For example, I want to get this value of 1.7655 from this page http://www.forexpros.com/currencies/usd-gel .

thanks

0
source share
4 answers

After calling the WPF component's Navigate WebBrowser method to open the web page, the DocumentCompleted event occurs and you can safely view the contents of the page (note that sometimes this event occurs several times). The Document WebBrowser property contains HTML in an already-processed format called the DOM tree. Unfortunately, you cannot easily use this property, since it is only an object . This feature was not completed in WPF (December 2011).

I would use a version of Winforms WebBrowser . You can use it in a WPF application if you embed it in WindowsFormsHost . This class is complete: its Document property is an HtmlDocument object with the Body property, which is an HtmlElement that contains the contents of the page. You can recursively traverse the DOM tree to find the element you need (and read it InnerText ), or simply process the text of the entire page using Regex or the HTML Parser Library .

+1
source

To get the WPF WebBrowser content, I found this solution somewhere, and it seems to work, but only if the target platform is at least .Net 4.0, and you have included Microsoft.CSharp.dll (which won’t be selected if your target structure is <4.0). I added it to LoadCompleted :

 private void myBrowser_LoadCompleted(object sender, NavigationEventArgs e) { dynamic doc = myBrowser.Document; dynamic htmlText = doc.documentElement.InnerHtml; string htmlstring = htmlText; } 

Add

 myBrowser.LoadCompleted += new LoadCompletedEventHandler(myBrowser_LoadCompleted); 

after InitializeComponent() to verify that the method is being called.

+5
source

You have several options for reading a value from a web page.

  • Get the page in a web browser control. Then try to find out if the element containing the required value has a specific name and gets this element from the document property of the webbrowser control.
  • The HtmlAgilityPack user will analyze the html of this web page to find the element and get the value from it.
  • Try to find out if the web page has a certain structure and uses a regular expression to find the desired value (it can be difficult!)

So, you see, you have many ways to find the value you need (and I think that these are not all options). So go ahead and take a little effort to get this value. And, if you have a question about a problem, feel free to ask again about the stack overflow. But please take the time to formulate your question. Remember: a good question will often get good answers!

+1
source

There is no general way to get the value from a random element - you need to know the HTML structure of a particular page and how to find the element you are looking for. But if you know both of them, you can read the page in some HTML document ( XmlDocument will work if there is a guarantee that the HTML will be structured correctly), and then get the value from there.

Perhaps you can start the page using some kind of HTML cleanup (maybe NTidy ?) And then load it into an XmlDocument. One of the drawbacks of this approach is to change the structure of the page during cleaning.

+1
source

All Articles