I want to write an windows form application who can do auto data filling in an textbox on the web page and get the corresponding data show on the page one by one. I met a problem that my loop goes too fast and I cannot see the result showing on the page. How can I let the page fully loaded and then go the next loop?
My designer has a textbox which is used to input url, webBrowser to browse the web page, the button is used to lunch the page.
My code are as below. I use "http://finance.yahoo.com/" as testing page. Testing data is in excel format. the data is a row, like " msft, bac, f, aapl".
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Net;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string url = textBox1.Text;
var request = (HttpWebRequest)WebRequest.Create(url);
var response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader sr = new StreamReader(stream);
string content = sr.ReadToEnd();
webBrowser1.DocumentText = content;
}
public void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElement tbUserid = webBrowser1.Document.GetElementById("mnp-search_box");
//HtmlElement tbPasswd = webBrowser1.Document.GetElementById("pwdInput");
HtmlElement btnSubmit = webBrowser1.Document.GetElementById("yucs-sprop_button");
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
if (tbUserid == null || btnSubmit == null)
{
return;
}
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='d:\\testing file\\list.xls';Extended Properties= \"Excel 8.0;HDR=Yes;IMEX=1\";");
OleDbDataAdapter da = new OleDbDataAdapter("select * from [List$]", con);
DataTable dt = new DataTable();
da.Fill(dt);
/* int i = 0;
do
{
string str = dt.Rows[i][0].ToString();
tbUserid.SetAttribute("value", str);
//System.Threading.Thread.Sleep(10000);
btnSubmit.InvokeMember("click");
evt.WaitOne();
i++;
}
while (i < dt.Rows.Count);
*/
for (int i = 0; i < dt.Rows.Count; i++)
{
string str = dt.Rows[i][0].ToString();
tbUserid.SetAttribute("value", str);
btnSubmit.InvokeMember("click");
Application.DoEvents();
//System.Threading.Thread.Sleep(100);
}
// ((WebBrowser)sender).Dispose();
}
}
}