Reputation: 2582
I need to get the updated html of webBrowser after it gets updated by an AJAX function
For example, I navigate to "page1.asp", but "page1.asp" includes an iframe that loads content from another page. It also has an jQoery function that display a Submit button after xx seconds.
My question is how can I get the most recent html from webBrowser?
I tried update but no luck,
webBrowser.Update();
Upvotes: 0
Views: 7090
Reputation: 2681
Well its a bit tricky question you need to follow the following guidelines correctly in order to achieve your goals.
write this code in the timer_tick event:
if (browser.ReadyState == WebBrowserReadyState.Complete)
Once you find the ready state completed then you look for the html element that you think will be updated after completion of ajax request.
You could do this way to check any element:
HtmlElementCollection forms = browser.Document.GetElementsByTagName("form");
HtmlElement form = null;
foreach (HtmlElement el in forms)
{
string name = el.GetAttribute("name");
if (name == "DATA")
{
form = el;
break;
}
}
4) Once you have got your element you may carry on your work.
Update: Here is the timer tick coding that you can extend as per your needs
Make sure you have set your Timer1.Inverval = 100
(100 milli seconds)
private void Timer1_Tick(sender, args)
{
Application.DoEvents();
// make sure your are pulling right element id
HtmlElement cTag = webBrowser.Document.GetElementById("myelement");
if(cTag != null) // if elemnt is found than its fine.
{
cTag.SetAttribute("value", "Eugene");
Timer1.Enabled = false;
}
else
{
// dont worry, the ajax request is still in progress... just wait on it and move on for the next tick.
}
Application.DoEvents(); // you can call it at the end too.
}
Upvotes: 1
Reputation: 7437
Use WebBrowser.DocumentText()
.
This property contains the text of the current document, even if another document has been requested. If you set the value of this property and then immediately retrieve it again, the value retrieved may be different than the value set if the WebBrowser control has not had time to load the new content. You can retrieve the new value in a DocumentCompleted event handler. Alternatively, you can block the thread until the document is loaded by calling the Thread.Sleep method in a loop until the DocumentText property returns the value that you originally set it to.
Try running calling DocumentText() inside a DocumentCompleted() event handler. If you can't do that because the iFrame doesn't throw this event, just set a timer and retrieve the text every few seconds.
Upvotes: 0