I am trying to write a code to download data from public data source to our server. In order to do that I wrote a code that opens the web page of data source website (WebBrowser Control named webBrowser1) then input the file number in the text box on the web page and click ok to get the result. Code worked fine for one file but then when I tried to do that in loop, InvokeMember("Click") only works when the loop is done (only clicks at the end of the loop). How can i change this code to click the button on webpage iteratively when Well_File_No changes? My code is attached below. Thanks for the help. I am writing the code using C# Windows Form.
private void runToolStripMenuItem_Click(object sender, EventArgs e)
{
//Code runs from Toolstrip on the Windows Form
for (int i = 0; i < 10; i++)
{
// this function try to locate textbox and click button on webpage
test(i);
}
}
void test(int i)
{
// File Number to be entered in textbox named "fileno"
Well_File_No = Convert.ToString(17141 + i);
webBrowser1.Document.GetElementById("fileno").SetAttribute("Value", Well_File_No);
foreach (HtmlElement btn in webBrowser1.Document.GetElementsByTagName("input"))
{
var BTN = btn;
if (BTN.GetAttribute("type").Equals("submit"))
{
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
BTN.InvokeMember("Click");
break;
}
}
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
... process data from the table and store....
}