[英]c# webbrowser control does not navigate to another page
我有一个控制台应用程序,并且在其中定义了一个Web浏览器。 首先,我导航到页面并填写登录表单,然后调用“提交”按钮进行登录。
之后,我想使用相同的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导航和InvokeScript的流程
但是,在尝试该实现之前,您可以简单地尝试在尝试导航到页面之前添加异步等待。 (异步等待类似于Thread.Sleep()
,但实际上并没有停止页面的加载,即“线程”)。
首先尝试:
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的WebBrowser或mshtml.HTMLDocument动态生成HTML代码? 我已经多次使用此代码神学,并且效果很好。
希望这些方法之一对您有所帮助! 让我知道,我可以帮助您生成一些更具体的代码段。
编辑:
乍一看,我猜想Console.ReadLine()
将冻结wb.Navigate("https://www.thewebsiteiwanttogo.com/product.aspx");
的导航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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.