C# WPF - 等待页面加载 [英] C# WPF - Waiting for page to load

查看:95
本文介绍了C# WPF - 等待页面加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 StackOverflow 上看到了几个关于这个主题的帖子,但似乎没有一个提供答案.

I have seen several threads on StackOverflow concerning this topic, however none of them seem to provide an answer.

我有一个按钮,当点击它时,会打开一个不可见的网页,导航到一个 URL,在一个框中输入信息,按下一个按钮,然后在屏幕上抓取信息.

I have a button that, when clicked, opens up an invisible web page, navigates to a URL, enters information into a box, presses a button, and then scrapes the screen for information.

我的代码骨干基本都在点击:

The bones of my code basically in the click:

WebBrowser wb = new WebBrowser;
wb.Visibility = System.Windows.Visibility.Hidden;
wb.Navigate("http://somepage.com");

这就是它变得棘手的地方.

And this is where it gets tricky.

我正在寻找一种方法来确保在尝试输入数据或抓取屏幕之前加载页面.我见过几个讨论 NavigatedIsLoadedLoadCompleted 以及 BackgroundWork 的话题,但我不能让其中任何一个工作.

I am looking for a way to ensure that the page is loaded before trying to enter data or scrape the screen. I have seen several threads that talk about Navigated, IsLoaded, LoadCompleted as well as BackgroundWork stuff, but I cannot get any of these to work.

用于确定页面已完全加载的最佳选项是什么?您将如何让所选方法发挥作用?

Which is the best option to use to determine that the page has fully loaded? How would you get the chosen method to work?

我也无法从屏幕获取数据,因为 WPF 不使用相同的 GetElementByID.

I also cannot get the data from the screen as WPF does not use the same GetElementByID.

根据下面的评论,这是我遇到的错误:

Per the comment below, here are the errors I run into:

  • 一旦页面也被导航,就首先导航,不一定要等到所有对象都加载完毕.这按预期工作,但不能用于我的目的.
  • IsLoaded 从不返回 true

private void GetData_Click(object sender, RoutedEventArgs e)
{
  int x=0;
  HTMLDocument doc;

  wb = new WebBrowser();
  wb.Visibility = System.Windows.Visibility.Visible;
  wb.Navigate("somesite.com");

  doc = wb.Document as mshtml.HTMLDocument;

  while(!wb.IsLoaded)
  {
    //Wait
  }

  doc.getElementById("txt_One").innerText = "It Worked";

}

将其置于无限循环中,因为 wb 似乎从未加载过.

Puts it in an infinite loop as wb does not ever seem to load.

  • 这是带有 LoadCompleted 的版本

事件 'System.Windows.Controls.WebBrowser.LoadCompleted' 只能出现在 += 或 -= 的左侧

The event 'System.Windows.Controls.WebBrowser.LoadCompleted' can only appear on the left hand side of += or -=

    private void GetData_Click(object sender, RoutedEventArgs e)
    {
      int x=0;
      HTMLDocument doc;

      wb = new WebBrowser();
      wb.Visibility = System.Windows.Visibility.Visible;
      wb.Navigate("somesite.com");

      doc = wb.Document as mshtml.HTMLDocument;

      wb.LoadCompleted += wb_LoadCompleted;

      doc.getElementById("txt_One").innerText = "It Worked";

    }

    void wb_LoadCompleted(object sender, NavigationEventArgs e)
    {

    }

产生错误

在 {filename} 中发生类型为System.NullReferenceException"的未处理异常

An unhandled exception of type 'System.NullReferenceException' occured in {filename}

附加信息:未将对象引用设置为对象的实例.

Additional information: Object reference not set to an instance of an object.

推荐答案

webbrowser 控件有一个加载事件(你有):LoadCompleted:当 dom 完全加载时触发.

The webbrowser control has a loadedevent (which you have): LoadCompleted: fires when the dom is fully loaded.

绑定事件并在事件方法中获取文档而不是立即获取.

Bind the event and in the event method get the document instead of right away.

    //root is a grid element identified in the XAML
    public WebBrowser webb;

    public MainWindow()
    {
        InitializeComponent();

        webb = new WebBrowser();
        webb.Visibility = System.Windows.Visibility.Hidden;
        root.Children.Add(webb);
        webb.LoadCompleted += webb_LoadCompleted;
        webb.Navigate("http://www.google.com");
    }

    void webb_LoadCompleted(object sender, NavigationEventArgs e)
    {
        MessageBox.Show("Completed loading the page");

        mshtml.HTMLDocument doc = webb.Document as mshtml.HTMLDocument;
        mshtml.HTMLInputElement obj = doc.getElementById("gs_taif0") as mshtml.HTMLInputElement;
        mshtml.HTMLFormElement form = doc.forms.item(Type.Missing, 0) as mshtml.HTMLFormElement;

        webb.LoadCompleted -= webb_LoadCompleted; //REMOVE THE OLD EVENT METHOD BINDING
        webb.LoadCompleted += webb_LoadCompleted2; //BIND TO A NEW METHOD FOR THE EVENT
        obj.value = "test search";
        form.submit(); //PERFORM THE POST ON THE FORM OR SEARCH
    }

    //SECOND EVENT TO FIRE AFTER YOU POST INFORMATION
    void webb_LoadCompleted2(object sender, NavigationEventArgs e)
    {
        MessageBox.Show("Completed loading the page second time after post"); 
    }

您需要将 doc = wb.Document 设为 mshtml.HTMLDocument;在 loadcompleted 事件中.因为在加载完成之前您无法获取文档.

You need to do doc = wb.Document as mshtml.HTMLDocument; in the loadcompleted event. Because until the load is complete you cannot get the document.

这篇关于C# WPF - 等待页面加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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