C#代码等待网页完成加载

时间:2013-07-03 19:03:13

标签: c# internet-explorer load webpage

我不能为我的生活弄清楚如何让我的C#代码等到网页完成加载。即使接近工作,我也无法获得ie.documentcompleted或ie.documentcomplete。事实上,即使我正在引用system.windows.forms程序集,我也无法让VS2012将documentcompleted作为下拉选项。请帮忙。

由于

public static string getTheBlockArray()
        {
            SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer();
            string id = "resultsTable";
            object Empty = 0;
            object URL = "http://www.cmegroup.com/clearing/trading-practices/block-data.html#contractTypes=FUT,OPT,SPD&exchanges=XCME&assetclass=assetClassId=1,6";
            IE.Navigate2(ref URL, ref Empty, ref Empty, ref Empty, ref Empty);

            // Some code here to wait until the webpage loads

            object theRange = IE.Document.getelementbyID(id).innertext;
            string blockString = theRange.ToString();
            IE.Quit();
            return blockString;
        }

3 个答案:

答案 0 :(得分:0)

试试这个:

while(IE.Busy)
{
    //You might want to sleep or count or something.
    //System.Threading.Thread.Sleep(1000);
}

答案 1 :(得分:0)

IE.Completed不起作用吗?

如果有效,你可以尝试做类似的事情:

IE.completed += (s, e) => {
  // your code

};

答案 2 :(得分:0)

通过异步请求由javascript代码填充的resultsTable元素的内容。因此,您不应该处理DOM,而是必须像javascript那样直接查询数据。以下代码可能是一个很好的起点。

  class Program
  {
    private static readonly string url = "http://www.cmegroup.com/clearing/trading-practices/CmeWS/mvc/xsltTransformer.do?xlstDoc=/XSLT/md/blocks-records.xsl&url=/da/BlockTradeQuotes/V1/Block/BlockTrades?exchange={0}&foi={1}&{2}&tradeDate={3}&sortCol={4}&sortBy={5}&_=1372913232800";

    static void Main(string[] args)
    {
      Exchange exchange = Exchange.XCME;
      ContractType contractType = ContractType.FUT | ContractType.OPT | ContractType.SPD;
      string assetClass = "assetClassId=0"; // See asset_class dropdown options in HTML source for valid values
      DateTime tradeDate = new DateTime(2013, 7, 3);
      string sortCol = "time"; // Column to sort
      SortOrder sortOrder = SortOrder.desc;

      string xml = QueryData(exchange, contractType, assetClass, tradeDate, sortCol, sortOrder);
      Console.WriteLine(xml);
    }

    private static string QueryData(Exchange exchange, ContractType contractType, string assetClass, DateTime tradeDate, string sortCol, SortOrder sortOrder)
    {
      string exc = GetEnumString(exchange);
      string ct = GetEnumString(contractType);
      string td = tradeDate.ToString("MMddyyyy");
      string query = string.Format(url, exc, ct, assetClass, td, sortCol, sortOrder.ToString());

      WebRequest request = WebRequest.Create(query);
      request.Credentials = CredentialCache.DefaultCredentials;
      using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
      {
        using (Stream stream = response.GetResponseStream())
        {
          using (StreamReader reader = new StreamReader(stream))
          {
            return reader.ReadToEnd();
          }
        }
      }
    }

    private static string GetEnumString(Enum item)
    {
      return item.ToString().Replace(" ", "");
    }
  }

  [Flags]
  enum Exchange
  {
    /// <summary>
    /// CBOT.
    /// </summary>
    XCBT = 1,

    /// <summary>
    /// CME.
    /// </summary>
    XCME = 2,

    /// <summary>
    /// COMEX.
    /// </summary>
    XCEC = 4,

    /// <summary>
    /// DME.
    /// </summary>
    DUMX = 8,

    /// <summary>
    /// NYMEX.
    /// </summary>
    XNYM = 16
  }

  [Flags]
  enum ContractType
  {
    /// <summary>
    /// Futures.
    /// </summary>
    FUT = 1,

    /// <summary>
    /// Options.
    /// </summary>
    OPT = 2,

    /// <summary>
    /// Spreads.
    /// </summary>
    SPD = 4
  }

  enum SortOrder
  {
    /// <summary>
    /// Ascending.
    /// </summary>
    asc,

    /// <summary>
    /// Descending.
    /// </summary>
    desc
  }

结果在xml变量(see)中。

祝你好运。