C#WebBrowser控件不导航到另一个页面 [英] c# webbrowser control does not navigate to another page

查看:285
本文介绍了C#WebBrowser控件不导航到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制台应用程序,我定义它里面的一个网页浏览器。
首先,我浏览到一个页面,并填写一个登录表单并调用提交按钮登录。



在这之后,我想转到另一个页面同一部位使用相同的web浏览器,但它不导航到该页面。 ,而是导航到它登录后重定向的页面



下面是我澄清代码;这段代码给我www.websiteiwanttogo.com/default.aspx代替product.aspx
这里有什么问题?



 <源代码code>静态web浏览器WB =新的WebBrowser(); 

[STAThread]
静态无效的主要(字串[] args)
{
wb.AllowNavigation = TRUE;
wb.Navigate(https://www.thewebsiteiwanttogo.com/login.aspx);
wb.DocumentCompleted + =新WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
Application.Run();


}

静态无效wb_DocumentCompleted(对象发件人,WebBrowserDocumentCompletedEventArgs E)
{
如果(wb.Url.ToString()。的IndexOf(login.aspx的)-1)〜
{
wb.Document.GetElementById(txtnumber)的setAttribute(价值,000001);
wb.Document.GetElementById(txtUserName)的setAttribute(价值,名为myusername);
wb.Document.GetElementById(txtPassword)的setAttribute(价值,输入mypassword);
wb.Document.GetElementById(btnLogin)InvokeMember(点击)。


}
,否则
{
//wb.Document.Body您登录任何你想在这里做。
wb.Navigate(https://www.thewebsiteiwanttogo.com/product.aspx);

Console.WriteLine(wb.DocumentText);
到Console.ReadLine();
Application.Exit();

}
}


解决方案

有很多不同的方法来完成这个功能。然而,我的猜测是:




  1. 无论是电话导航到下一个页面正在发生的太快,或

  2. Document.Completed 事件不能正常登录后射击(这是常见的,特别是如果目标文档包含动态脚本)



我已经做了很多的网页自动化(从链接导航到链接,然后执行一些动作,然后导航到另一个链接等),你应考虑使用异步流程。原则上,它可能总是最好的时候,处理的 web浏览器对象使用异步流程,只是因为有你需要一个进程的同时执行其他功能运行多个实例



没有进入过多的细节,看这个问题的答案,并研究代码:的 web浏览器导航的流量和invokeScript



努力的实现之前,但是,你可以简单地尝试等待试图浏览网页前添加一个异步。 (异步等待类似于 Thread.sleep代码(),但实际上并没有停止页面的加载,即线程)。



(从来没有听说过异步进程之前,请查看本教程在MSDN上



首先尝试此:

 静态无效wb_DocumentCompleted(对象发件人,WebBrowserDocumentCompletedEventArgs E)
{
如果(wb.Url.ToString()的IndexOf(login.aspx的)方式> -1)
{
wb.Document.GetElementById(txtnumber)的setAttribute(价值,000001);
wb.Document.GetElementById(txtUserName)的setAttribute(价值,名为myusername);
wb.Document.GetElementById(txtPassword)的setAttribute(价值,输入mypassword);
wb.Document.GetElementById(btnLogin)InvokeMember(点击)。


}
,否则
{
//wb.Document.Body您登录任何你想在这里做。
等待Task.Delay(1000); //等待1秒只让WB赶上
wb.Navigate(https://www.thewebsiteiwanttogo.com/product.aspx);

Console.WriteLine(wb.DocumentText);
到Console.ReadLine();
Application.Exit();

}
}

如果这没有帮助,考虑 上面的链接,并尝试实现与异步流程更强大的导航序列。



如果不工作,并且希望一些帮助航行通过或等待动态页面加载,试试这个帖子:的how使用.NET的web浏览器动态生成HTML代码或mshtml.HTMLDocument?
我用这个代码神学很多次,它的伟大工程。



希望这些方法之一帮助!让我知道,我可以帮助你产生一些更具体的代码片段



编辑:



在第二看起来,我要去猜测,到Console.ReadLine()是要冻结的 wb.Navigate(HTTPS的导航:/ /www.thewebsiteiwanttogo.com/product.aspx); ,因为它不会立即发生。你可能会想在 Document.Completed 处理程序添加另一个如果语句让 wb.Navigate(https://www.thewebsiteiwanttogo.com/product.aspx); 来完成导航试图抢前 wb.DocumentText 。例如:

 静态无效wb_DocumentCompleted(对象发件人,WebBrowserDocumentCompletedEventArgs E)
{
如果(WB。 Url.ToString()的IndexOf(login.aspx的)方式> -1)
{
wb.Document.GetElementById(txtnumber)的setAttribute(价值,000001);
wb.Document.GetElementById(txtUserName)的setAttribute(价值,名为myusername);
wb.Document.GetElementById(txtPassword)的setAttribute(价值,输入mypassword);
wb.Document.GetElementById(btnLogin)InvokeMember(点击)。
}
,否则如果(wb.Url.ToString()的IndexOf(product.aspx)方式> -1)
{
Console.WriteLine(wb.DocumentText) ;
到Console.ReadLine();
Application.Exit();
}
,否则
{
//wb.Document.Body您登录任何你想在这里做。
等待Task.Delay(1000); //等待1秒只让WB赶上
wb.Navigate(https://www.thewebsiteiwanttogo.com/product.aspx);
}
}


I have a console application and i've defined a webbrowser inside it. Firstly, i navigate to a page and fill a login form and invoke the submit button to login.

After that, i want to go to another page in the same site using the same webbrowser but it does not navigate to that page. instead, it navigates to the page that it's redirected after login.

Here is my code for clarification; this code gives me the source code of www.websiteiwanttogo.com/default.aspx instead of product.aspx what is wrong here?

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();

        }
    }

解决方案

There are a lot of different ways to accomplish this functionality. However, my guess is that:

  1. Either the call to navigate to the next page is happening too quickly, or
  2. The Document.Completed event is not firing properly after logging in (this is common especially if the destination document contains dynamic scripts)

I've done a lot of web page automating (navigating from link to link, then performing some actions, then navigating to another link, etc.), and you should consider using async processes. In principle, it is probably always best when dealing with the webBrowser object to use async processes, simply because there are many instances where you need one process to run while you perform other functions.

Without going into too much detail, look at the answer to this question and study the code: Flow of WebBrowser Navigate and InvokeScript

Before trying that implementation, however, you could simply try adding an async await before trying to navigate to the page. (async await is similar to a Thread.Sleep(), but doesn't actually stop the loading of the page, i.e. the "thread").

(Never heard of asynchronous processes before? Check out this tutorial on MSDN).

Try this first:

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();

    }
}

If this doesn't help, consider the link above and try implementing a more robust navigating sequence with async processes.

If that doesn't work, and you'd like some help navigating through or waiting for dynamic pages to load, try this post: how to dynamically generate HTML code using .NET's WebBrowser or mshtml.HTMLDocument? I've used this code theology many times, and it works great.

Hope one of these methods helps! Let me know, and I can help you generate some more specific code snippets.

EDIT:

At second glance, I'm going to guess that the Console.ReadLine() is going to freeze up the navigating of wb.Navigate("https://www.thewebsiteiwanttogo.com/product.aspx");, since it won't happen instantaneously. You'll probably want to add another if statement in the Document.Completed handler to allow wb.Navigate("https://www.thewebsiteiwanttogo.com/product.aspx"); to finish navigating before trying to grab the wb.DocumentText. For example:

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");
    }
}

这篇关于C#WebBrowser控件不导航到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆