c# generates JS to fill webform: how to verify it works?
as continuation to:
WebBrowser Control in a new thread
how to fill an external web form with c# application?
I want to fill a website (i.e. gmail) web-form. I thought to avoid the WebBrowser control overhead. I used JS. but the following code doesn't work (as the Navigation_completed handler is called too many times). How can I verify the following code works? Is there any faster way to do so ?
static void Main(string[] args)
{
runBrowserThread(new Uri("h开发者_开发问答ttps://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F%3Fui%3Dhtml%26zy%3Dl&bsv=llya694le36z&ss=1&scc=1<mpl=default<mplcache=2&hl=iw&from=logout"));
}
private static void runBrowserThread(Uri url)
{
var th = new Thread(() =>
{
var br = new WebBrowser();
br.DocumentCompleted += browser_DocumentCompleted;
br.Navigate(url);
br.Navigate("javascript: void(document.getElementsByName('Email')[0].value = 'Name';document.getElementsByName('Passwd')[0].value = 'Pass'; document.getElementsByName('signIn')[0].click();)");
Application.Run();
});
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
static void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var br = sender as WebBrowser;
if (br.Url == e.Url)
{
Console.WriteLine("Natigated to {0}", e.Url);
// Application.ExitThread(); // Stops the thread
}
}
TIA
精彩评论