I'm attempting to write a web scraping app and am having problems with a site that runs some JavaScript to generate the data I require after the page has loaded.
The page runs this javascript when the page has finished loading:
$(document).ready( function() {
$("#periodSelect, #typeSelect").change(spotSystemPrice.load);
spotSystemPrice.load();
When that finishes it populates a div ( id="spotSystemPriceOutput") with the data.
I tried doing this using only the WebBrowser class, but the InvokeScript only lets you call functions, not invoke methods. The closest solution I have found so far is to insert some javascript using MSHTML.dll which invokes the method. This seems to be working, but I need some help loading the div output into a string, or I can even work with the full body html. I'm very new to C# so am completely out of my depth with this and I think the final step I need is going to be really easy, so I just need one of you experts to help me along :)
Here is the code I'm working with. Any suggestions to help me finish it, or even a completely different solution would be greatly appreciated.
WebBrowser wb = new WebBrowser();
wb.Navigate(URL);
while (wb.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
var doc = (IHTMLDocument2)wb.Document.DomDocument;
var headItems = (IHTMLElementCollection)doc.all.tags("head");
var scriptObject = (IHTMLScriptElement)doc.createElement("script");
scriptObject.type = @"text/javascript";
scriptObject.text = "spotSystemPrice.load();";
var node = (IHTMLDOMNode)headItems.item(null, 0);
node.appendChild((IHTMLDOMNode)scriptObject);
Interestingly, If I change my javascript injection to "spotSystemPrice.load(); alert('');" After clicking ok on the message box, I can see the results in the object explorer using the text visualiser which gives me an expression reference of ((((mshtml.HTMLHeadElementClass)(node)).document).body).innerHTML. How would adding alert to the javascript change my results? Do I need to wait for some kind of onComplete event?
Update: I also found this which looked useful Calling javascript object method using WebBrowser.Document.InvokeScript and I modified my code to:
WebBrowser wb = new WebBrowser();
wb.Navigate(URL);
while (wb.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
string JScript = "spotSystemPrice.load();";
object[] args = { JScript };
wb.Document.InvokeScript("eval", args);
while (wb.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
but that still gives me no data in the div element. But for some reason if I chance my javascript to "alert('');" and don't even try to invoke the method, the data I need is there! what's going on? I'm so confused.