【问题标题】:c# webbrowser control does not navigate to another pagec# webbrowser 控件不导航到另一个页面
【发布时间】:2015-05-31 16:17:43
【问题描述】:

我有一个控制台应用程序,并在其中定义了一个网络浏览器。 首先,我导航到一个页面并填写登录表单并调用提交按钮进行登录。

之后,我想使用相同的网络浏览器转到同一站点中的另一个页面,但它没有导航到该页面。相反,它会导航到登录后重定向的页面。

这是我的澄清代码;这段代码给了我 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();

        }
    }

【问题讨论】:

  • 感谢您提出如此精彩的问题。看到这个问题和答案后,我可以看到外卖..

标签: c# webbrowser-control navigateurl


【解决方案1】:

有很多不同的方法可以实现此功能。但是,我的猜测是:

  1. 导航到下一页的调用发生得太快,或者
  2. 登录后Document.Completed 事件未正确触发(这很常见,尤其是在目标文档包含动态脚本时)

我已经完成了很多网页自动化(从一个链接导航到另一个链接,然后执行一些操作,然后导航到另一个链接等),您应该考虑使用异步进程。原则上,在处理 webBrowser 对象时使用异步进程可能总是最好的,因为在许多情况下,您需要在执行其他功能的同时运行一个进程。

不讲太多细节,看这个问题的答案,研究代码:Flow of WebBrowser Navigate and InvokeScript

但是,在尝试该实现之前,您可以简单地尝试在尝试导航到页面之前添加异步等待。 (异步等待类似于Thread.Sleep(),但实际上并不停止页面的加载,即“线程”)。

(以前从未听说过异步进程?查看this tutorial on 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();

    }
}

如果这没有帮助,请考虑 the link above 并尝试使用异步进程实现更强大的导航序列。

如果这不起作用,并且您需要一些帮助来浏览或等待动态页面加载,请试试这个帖子:how to dynamically generate HTML code using .NET's WebBrowser or mshtml.HTMLDocument? 我已经多次使用这个密码神学,效果很好。

希望这些方法之一有所帮助!告诉我,我可以帮你生成一些更具体的代码 sn-ps。

编辑:

乍一看,我猜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");
    }
}

【讨论】:

  • 感谢本的回答!我将尝试您在 EDIT 之后添加的示例中的代码,但似乎我必须升级到 VS2012 才能使用它,因为我不能在 2010 年使用“等待”,而且 Microsoft.Bcl.Async 和 Async CTP 都不适合我。应用您的解决方案后,我将对其进行编辑。
  • 我会先在没有await 的情况下尝试一下,看看是否可行。如果没有,您肯定必须使用异步方法。
  • 是的,你是对的。它在没有 await 命令的情况下工作。但它给出了一些脚本错误并询问我是否继续工作。如果我希望程序继续运行,我必须单击“是”。我可以让它自动吗?
  • 好的,我解决了。如This question 所述,我禁用了脚本错误。我会将您的答案标记为解决方案。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2017-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
  • 1970-01-01
  • 2010-11-29
相关资源
最近更新 更多