我有一个控制台应用程序,我在其中定义了一个WebBrowser。
首先,我导航到一个页面并填写登录表单,然后调用提交按钮进行登录。
之后,我想使用同一WebBrowser转到同一站点的另一个页面,但它没有导航到该页面。相反,它导航到登录后重定向的页面。
以下是我的代码以进行澄清;此代码给出了www.websiteiwanttogo.com/default.aspx的源代码,而不是product.aspx。这里有什么问题?
之后,我想使用同一WebBrowser转到同一站点的另一个页面,但它没有导航到该页面。相反,它导航到登录后重定向的页面。
以下是我的代码以进行澄清;此代码给出了www.websiteiwanttogo.com/default.aspx的源代码,而不是product.aspx。这里有什么问题?
static WebBrowser wb = new WebBrowser();
[STAThread]
static void Main(string[] args)
{
wb.AllowNavigation = true;
wb.Navigate("https://www.thewebsiteiwanttogo.com/login.aspx");
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
Application.Run();
}
static void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (wb.Url.ToString().IndexOf("login.aspx") > -1)
{
wb.Document.GetElementById("txtnumber").SetAttribute("value", "000001");
wb.Document.GetElementById("txtUserName").SetAttribute("value", "myusername");
wb.Document.GetElementById("txtPassword").SetAttribute("value", "mypassword");
wb.Document.GetElementById("btnLogin").InvokeMember("click");
}
else
{
//wb.Document.Body you are logged in do whatever you want here.
wb.Navigate("https://www.thewebsiteiwanttogo.com/product.aspx");
Console.WriteLine(wb.DocumentText);
Console.ReadLine();
Application.Exit();
}
}