Reputation: 34158
How can I get the element value from the web page in C#, with the WPF WebBrowser component?
For example I want to get this value 1.7655 from this page http://www.forexpros.com/currencies/usd-gel.
Thanks
Upvotes: 1
Views: 10768
Reputation: 443
For getting the WPF WebBrowser
's content I found this solution somewhere and this seems to work, but only if the target Framework is at least .Net 4.0 and you include Microsoft.CSharp.dll
(which won't be selectable if your target framework is <4.0). I added it in the 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 be sure the method is called.
Upvotes: 5
Reputation: 28688
After you call the Navigate
method of the WebBrowser
component of WPF to open a webpage, the DocumentCompleted
event arrives, and you can safely browse the content of the page (note that sometimes this event occurs multiple times). The Document
property of WebBrowser
contains the HTML in an already processed format, called the DOM tree. Unfortunately, you cannot use this property easily, since it is only an object
. This feature has not been completed in WPF (December 2011).
I would use the Winforms version of WebBrowser
instead. You can use it in a WPF application if you embed it into a WindowsFormsHost
. This class is complete: its Document
property is an HtmlDocument
object, with a Body
property, which is an HtmlElement
, which contains the content of the page. You can walk the DOM tree recursively to find the element you want (and read its InnerText
), or simply process the text of the whole page using Regex or an HTML parser library.
Upvotes: 1
Reputation: 12458
You have several options to read a value from a webpage.
So, you see, you have many ways to find your desired value (And I think that are not all options). So, go ahead and spend some effort to get that value. And, if you got a question about a certain problem, don't hesitate and ask again on Stack Overflow. But please, spend some time in formulating your question. Remember: A good question will very often get good answers!
Upvotes: 0
Reputation: 3985
There won't be a generic way to get a value from a random element - you need to know the HTML structure of the specific page, and how to find the element you are looking for. But if you know both of those, you can read the page into some sort of an HTML document (XmlDocument
would work if there was a guarantee that the HTML will be structured properly) and then get the value from there.
Optionally you can run the page through some sort of HTML cleanup (maybe NTidy?) and then load it into an XmlDocument. One drawback of such an approach is the structure of the page may change during the cleanup.
Upvotes: 1