我有一个控制台应用程序,并在其中定义了一个Web浏览器。首先,我导航到一个页面,填写一个登录表单,并调用提交按钮进行登录。
之后,我想使用相同的网络浏览器转到同一网站中的另一个页面,但它无法导航到该页面。相反,它会导航到登录后重定向的页面。
这是我需要澄清的代码;该代码为我提供了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();
}
}
有很多不同的方法可以实现此功能。然而,我的猜测是:
- 导航到下一页的调用发生得太快,或者
- 登录后
Document.Completed
事件未正确激发(这很常见,尤其是当目标文档包含动态脚本时)
我已经完成了很多网页自动化(从一个链接导航到另一个链接,然后执行一些操作,然后导航到另个链接,等等),您应该考虑使用异步进程。原则上,在处理webBrowser
对象时,使用异步进程可能总是最好的,因为在许多情况下,在执行其他功能时需要运行一个进程。
不需要过多的细节,看看这个问题的答案并研究代码:WebBrowser导航和调用脚本的流程
然而,在尝试该实现之前,您可以简单地尝试在尝试导航到页面之前添加一个异步等待。(异步等待类似于Thread.Sleep()
,但实际上并没有停止页面的加载,即"线程")。
(以前从未听说过异步进程?请查看MSDN上的本教程)。
先试试这个:
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.
await Task.Delay(1000); //wait for 1 second just to let the WB catch up
wb.Navigate("https://www.thewebsiteiwanttogo.com/product.aspx");
Console.WriteLine(wb.DocumentText);
Console.ReadLine();
Application.Exit();
}
}
如果这没有帮助,请考虑上面的链接,并尝试使用异步进程实现更健壮的导航序列。
如果这不起作用,并且您希望在导航或等待加载动态页面时获得一些帮助,请尝试以下文章:如何使用.NET';动态生成HTML代码;s WebBrowser或mshtml。HTML文档?我已经用了很多次这种神学密码,而且效果很好。
希望这些方法中的一个有帮助!让我知道,我可以帮助您生成一些更具体的代码片段。
编辑:
乍一看,我猜Console.ReadLine()
会冻结wb.Navigate("https://www.thewebsiteiwanttogo.com/product.aspx");
的导航,因为它不会立即发生。您可能需要在Document.Completed
处理程序中添加另一条if
语句,以允许wb.Navigate("https://www.thewebsiteiwanttogo.com/product.aspx");
在尝试获取wb.DocumentText
之前完成导航。例如:
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 if(wb.Url.ToString().IndexOf("product.aspx") > -1)
{
Console.WriteLine(wb.DocumentText);
Console.ReadLine();
Application.Exit();
}
else
{
//wb.Document.Body you are logged in do whatever you want here.
await Task.Delay(1000); //wait for 1 second just to let the WB catch up
wb.Navigate("https://www.thewebsiteiwanttogo.com/product.aspx");
}
}