I can not for the life of me figure out how to get my C# code to wait until the webpage finishes loading. I can't get the ie.documentcompleted or ie.documentcomplete even close to working. In fact even though I'm referencing the system.windows.forms assembly, I can't get VS2012 to give me documentcompleted as a dropdown option. Please help.
Thanks
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;
}
Try this:
while(IE.Busy)
{
//You might want to sleep or count or something.
//System.Threading.Thread.Sleep(1000);
}
does not IE.Completed works?
if iy works, you could try do something like that:
IE.completed += (s, e) => {
// your code
};
The content of resultsTable element filled by a javascript code via an async request. So you shouldn't process the DOM, instead you have to directly query the data, as the javascript does. This code below could be a good starting point.
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
}
The result is in the xml variable ( see ).
Good luck.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.