コンソール アプリケーションがあり、その中に Web ブラウザを定義しました。まず、ページに移動してログインフォームに入力し、送信ボタンを呼び出してログインします。
その後、同じ Web ブラウザを使用して同じサイト内の別のページに移動したいのですが、そのページに移動しません。代わりに、ログイン後にリダイレクトされるページに移動します。
明確にするための私のコードは次のとおりです。このコードは、product.aspx の代わりに www.websiteiwanttogo.com/default.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();
}
}