i have project in web by automate a website, input data, after that submit this site and get data when it'd responed. surf in google and i've decided choose the best acticle in codeproject using Webbrowser Control.
http://www.codeproject.com/Articles/50544/Using-the-WebBrowser-Control-in-ASP-NET
There a so much problems which i can not trace and try alot by finding solutions by google, but nothing change.
The site i want to extract data have the submit form as below:
<form id = "ctl103">
<input type="radio" id="rdoFlightTypeReturn" name="rdoFlightType"
value="return" checked="checked"/>
<input type="radio" id="rdoFlightTypeOneWay" name="rdoFlightType"
value="oneway" />
<input name="so long" type="text" id="txtOrigin" autocomplete="off" />
<input name="very much" type="text" id="txtDestination" autocomplete="off" />
<input type="text" id="txtDepart" autocomplete="off" name="txtDepart"/>
//dd/mm/yyyy format
<input type="text" id="txtReturn" autocomplete="off" name="txtReturn" />
//dd/mm/yyyy format
<button type="submit" class="png-bg" id="btnSearchForFlights">Search Flight</button>
</form>
The code i write to submit this form:
void IEBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlDocument doc = ((WebBrowser)sender).Document;
if (doc.Title.Equals("Home") && loginCount++ < 3)
{ try
{
HtmlElement element = doc.GetElementById("rdoFlightTypeOneWay");
element.InvokeMember("click");
}
catch {
ieBrowser.Navigate("http://www.my---favoritesite.com");
return;
}
doc.GetElementById("txtOrigin").InnerText = "SGN";
doc.GetElementById("txtDestination").InnerText = "HAN";
doc.GetElementById("txtDepart").InnerText = "03/11/2012";
doc.InvokeScript("setTimeout", new object[] { "submitFlights()", 20 });
}
else
{
doc.InvokeScript("setTimeout", new object[] {
string.Format("window.external.getHtmlResult({0})", navigationCounter), 10 });
}
}
The problem is:
Some time it's work, very usually when few first time i run the project, but after that, it's not ok. I thinks the reason was i can not submit or Postback wrong way, so i try something like that:
HtmlElement button_searchFlight = doc.GetElementById("btnSearchForFlights");
button_searchFlight.InvokeMember("click");
or
mshtml.HTMLAnchorElementClass obj =
(HTMLAnchorElementClass)button_searchFlight.DomElement;
obj.click();
or
mshtml.HTMLButtonElementClass button1 =
(mshtml.HTMLButtonElementClass)button_searchFlight.DomElement;
button1.click();
or
doc.InvokeScript("submitFlight()");
or doc.GetElementById("ctl03").InvokeMember("submit");
all those stuff go wrong or reset the form or do nothing, so i've come back with
doc.InvokeScript("setTimeout", new object[] { "submitFlights()", 20 });
to submit the form.
Note: one thing so strange is: when i finding in the web, i've founded "submitFlight()" can work. It fired the bookNow() and after that, the bookNow() fired another script "formsubmit()", but i can not find the "formsubmit()" anywhere in source code including some other javascript link to the page.
Because i've successed sometime to run the project, and now, it also do not post any data to the site, so i think the problem may be in cookies which the site set before. I've go to IE (internet explorer) to delete all cookies. After that, the code worked again to submit input data but do wrong way. I've founded that some cookies still set, it was about the old state of textbox"txtDepart" set "1/11/2012"
All i can figured out my problem may be about the javascript oF the site or/and the cookies it set.
(sorry because my poor English)