C# how to wait for a webpage to finish loading before continuing
Try the DocumentCompleted
Event:
webBrowser.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser.Document.GetElementById("product").SetAttribute("value", product);
webBrowser.Document.GetElementById("version").SetAttribute("value", version);
webBrowser.Document.GetElementById("commit").InvokeMember("click");
}
C# how to wait for a webpage to finish loading before continuing in watin
I solved my problem using WaitUntilExists()
method. and I add a 60 seconds time out for the WaitUntilExists()
because the default time out is 30 seconds and because of my slow speed of internet I replaced it with 60 seconds like this browser.Div("div1").WaitUntilExists(60);
C# - Wait for a WebBrowser to completely finish navigating and loading a website/webpage
Try to add an event listener to WebBrowser. The WebBrowser has a WebBrowser.DocumentCompleted event that occurs when the web page has been fully loaded.
Something like
public frmMain()
{
website.DocumentCompleted += website_DocumentCompleted;
}
public void website_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
website.Document.GetElementById("search").InnerText = "Cavaliers vs Boston highlights"
}
where frmMain is your form. It could be of course added in somewhere else too.
How to make WebBrowser wait till it loads fully?
Add This to your code:
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
Fill in this function
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
//This line is so you only do the event once
if (e.Url != webBrowser1.Url)
return;
//do you actual code
}
C# Making program wait until browser finish loading webpage
You can't. The problem here is that Firefox does not communicate back when the web page has loaded. You will for example see issues when Firefox wants to update itself before it opens the web page. How is it going to communicate back when the page is loaded? In between, the entire firefox.exe executable has been replaced, and the connection with your Process
has been long lost.
The WaitForInputIdle
does a very specific job, and this is not what you expect. Windows works through a message pump. When you e.g. move the mouse over a window, a message is send to that window. WaitForIdleInput
returns when the application has processed the first message it has received, so when Windows knows it is 'responsive'.
C# code to wait until webpage finishes loading
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.
Waiting for page to be fully loaded using WatiN
I have ran into similar problem with watin on a site using Ajax.
This is the workaround for this.
//After click on link/Tab/Button on which the result is loaded in non Ajax websites.
We have a function here, browser.WaitForComplete()
but it works only when the page is in loading state. but in case of Ajax on a part of browser window gets updated. so no loading state for browser.
So one solution for this problem is
Use Thread.Sleep(10000);
This time can vary upon the normal time the website takes to load the required div.
How can I wait for page load in selenium before moving to another page? (C#)
bool wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(60)).Until(d => ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").Equals("complete"));
if(wait == true)
{
//Your code
}
Above code will wait for page to load for 60 seconds and return true if page is ready(within 60 seconds), false if page is not ready (after 60 seconds).
Related Topics
How to Sort a String of Text Followed by a Number Using Linq
Objects in Scene Dark After Calling Loadscene/Loadlevel
How to Pass Data from Mainwindow to a User Control That's Inside the Mainwindow
Shellexecute Equivalent in .Net
Print Webbrowser Without Previewing I.E. Single Click Print
Datetime Conversion from String C#
How to Make the Value of a Variable Track the Value of Another
How to Change the Background Color of All Other Forms from One Form
Repository Pattern Step by Step Explanation
What Is the Fastest Way to Create a Checksum for Large Files in C#
How to Run and Interact with an Async Task from a Wpf Gui
Identityserver4 Register Userservice and Get Users from Database in ASP.NET Core
Dynamic Routes from Database for ASP.NET MVC Cms
How to Initialize a C# Dictionary with Values
Why the Default Synchronizationcontext Is Not Captured in a Console App