c# - Webbrowser in ASP.Net - accessing DOM of loaded page -
i'm trying screen scrape web page "http://reading.beatthestreet.me/" type in card number , displays point score - done via ajax (try card number 123456)
i trying via webbrowser control in asp.net page. seems load ok bit scrape ajax generated results doesn't work - return blank
the class here
using system; using system.collections.generic; using system.linq; using system.web; using system.threading; using system.windows.forms; namespace beat_the_street { public class customwebbrowser { protected string _url; protected string _cardnumber; string score = ""; public string getcardscore(string cardnumber) { _url = "http://reading.beatthestreet.me/"; _cardnumber = cardnumber; // webbrowser activex control must run in // single-threaded apartment create thread create // control , generate thumbnail thread thread = new thread(new threadstart(getcardscoreworker)); thread.setapartmentstate(apartmentstate.sta); thread.start(); thread.join(); string s = score; return s; } protected void getcardscoreworker() { using (webbrowser browser = new webbrowser()) { // browser.clientsize = new size(_width, _height); browser.scrollbarsenabled = false; browser.scripterrorssuppressed = true; browser.navigate(_url); // wait control load page while (browser.readystate != webbrowserreadystate.complete) application.doevents(); //set text of search input htmlelement txttextfield = browser.document.getelementbyid("edit-card-number"); txttextfield.innertext = _cardnumber; system.threading.thread.sleep(1000); //perform click on search button htmlelement btnsubmit = browser.document.getelementbyid("edit-submit"); btnsubmit.focus(); btnsubmit.invokemember("click"); // sleep allow population (int = 0; < 30; i++) { system.threading.thread.sleep(1000); htmlelement resultsboxtemp = browser.document.getelementbyid("box"); if (resultsboxtemp != null) { system.threading.thread.sleep(3000); = 35; // exit } } // results htmlelement resultsbox = browser.document.getelementbyid("box"); if (resultsbox != null) { string results = resultsbox.innertext; if (results != null) { string pointsscore; pointsscore = results.replace("\r", "").replace("\n", "").replace("you have ", ""); pointsscore = pointsscore.substring(0, pointsscore.indexof(" ")); score = pointsscore; } } } } } }
it gets called this
customwebbrowser browser = new customwebbrowser(); string s = browser.getcardscore("326325");
Comments
Post a Comment