Wait for window.onload event using Selenium Webdriver - c#

I need a solution how to wait until the web page is fully loaded. And exactly after that, waiting no longer, I need to perform actions with a web page: get URL of loaded page, check cookie, click <a> elements so on
I use the following code before work with loaded page:
IWait<IWebDriver> wait=new WebDriverWait(drv, TimeSpan.FromSeconds(30.00));
wait.Until(d => ((IJavaScriptExecutor)drv).ExecuteScript("return document.readyState").Equals("complete"));
But this code does not wait in 100% cases until full load of a page in Chrome, and does not work in Internet Explorer!
Could you please offer better solution
Thanks for an answer in advance!

document.readyState does not always return the actual status of page load completion. see if you can wait on a particular element on the page, perhaps which gets loaded last on the page.

Related

cefSharp Detect when entire page is loaded

I need to be able to detect when an entire page is loaded. The page has JavaScript that executes when you browse to it as well. And I must wait for that to finish loading as it adds HTML Elements to the page.
I have tried the below event. But this is trigger when the main page loads and doesn't care about the javascript.
browser.LoadingStateChanged
So I also tried count these to know when all the frames on a page where loaded. But it still wasn't spot on.
browser.FrameLoadStart += OnFrameLoadStart;
browser.FrameLoadEnd += OnFrameLoadEnd;
Any suggestions
Ended up using javascript to wait for element to display and then continue

Wait till the GeckoFX Webbrowser has loaded

I want to automate a few tasks on my website with GeckoFX for some testing.
That should happen when I click a button and everything should be automated after that button click.
This includes clicking buttons where the page refreshes so the code has to wait till the page has loaded and that's where my problem is.
If I do it like that:
geckoWebBrowser1.Navigate("http://mywebsite.com");
GeckoInputElement searchText = new GeckoInputElement(geckoWebBrowser1.Document.GetElementsByName("searchbox")[0].DomObject);
searchText.Value = "GeckoFx";
I get an error, so how can I put it that the code after .Navigate waits till the webbrowser has fully loaded the page?
You can use DocumentCompleted Method to perform your automatic operations.
private void geckoWebBrowser1_DocumentCompleted(object sender,EventArgs e)
{
// Here you can add the coding to perform after document loaded
}
For example : First initiate the browser to google page by geckoWebBrowser1.Navigate("https://www.google.com");
After google page loaded you can do the following in document_completed method:
GeckoInputElement search =new GeckoInputElement(geckoWebBrowser2.Document.GetElementsByName("q")[0].DomObject);
GeckoInputElement button = new GeckoInputElement(geckoWebBrowser2.Document.GetElementsByName("btnG")[0].DomObject);
search.focus();
search.Value = "Master Blaster Sachin";
button.Click();
so it will search the value you given automatically after the google page loaded. Like that you can modify the program as per your logic. Hope it helps..
I would go an use a product like Selenium http://seleniumhq.org/. It's free open source web testing which is scriptable.

How to not wait for a page load after SelectElement.SelectByValue()

Given a page with a <select> element and some <option>s, that when clicked cause the browser to open a new URL, like (JavaScript code omitted):
<select>
<option value="here">go here</option>
<option value="there">go there</option>
</select>
When I automate this behavior with WebDriver I call
hereOrThereSelectElement.SelectByValue("here");
and then it waits until the new page has loaded.
In my case, the page load for the new location takes more than 60s (intentionally), so I had to increase the command timeout of the WebDriver instance. Unfortunately this timeout will affect all other tests created with the same WebDriver instance.
Also I would like to perform an explicit Wait for the new page to load (to mark it as long running in the test scenario), but in this condition the next command will be called after the SelectByValue command succeeded - so no Wait needed anymore.
I looked into the source and realized that SelectByValue command uses the Click logics which waits for a page to load if needed.
So my question is: How do I explicitly perform a SelectByValue (or Click) without waiting for a new page to load?
Update:
What I actually want to have in my code is:
// the following command should not wait for the new page to load
hereOrThereSelectElement.SelectByValueButDoNotWait("here");
webDriverWait.Until(/* some "page has loaded" check */)
Finally, I found a solution for this.
Meanwhile the problem has slightly shifted to a call to a WebElement's Click() method that ran into the timeout. Nethertheless the following solution applies also to the original problem with calling SelectByValue().
So, one can use Selenium's Actions low-level interactions builder to perform a click on an element.
var actions = new Actions(webDriver);
actions
.MoveToElement(aClickableWebElement);
.Click();
.Build()
.Perform();
Maybe this will help anyone having similar problems.

How to determind if the web page is loaded completely in web browser?

I am working on windows form application which is HTML base user interface.
I need to know when the web page is loaded completely.
I've tested many web browser events like DocumentCompleted, IsBusy, ReadyState but none of them responded what i expected.
If you can use the jQuery library, then it's really simple.
$(document).ready() {
//your page is fully loaded
};
Otherwise you'll have to have to rely on different methods based on the browser you're using. Since it's a windows form application, I'm assuming the rendering engine you're using is IE based. If that's then this might work for you:
if (document.attachEvent)
{
document.attachEvent("onreadystatechange", function()
{
if (document.readyState === "complete")
{
document.detachEvent("onreadystatechange",
arguments.callee);
/* code to run on load */
}
});
}
You can find other browser dependent solutions here, if you're interested:
http://dean.edwards.name/weblog/2006/06/again/
Chamika Sandamal is correct - you should use DocumentComplete event BUT - check the 'sender' object. The very last 'complete' event is coming from 'browser' object itself and not from images, text, etc.. that fire it on loading. After all elements on page will fire DocumentComelete event the very last event will come from browser itself. If you could cast the 'sender' to browser object - here you go - it's browser loading complete event. Just notice that in case you have any 'frame' tags in HTML they will rise different DcoumentComplete events after browser Complete event. I think 'frame' considered as another HTML page so it will have itself 'complete' events..

Fire Event after Page Close or Redirected to other page

I have some problems with ASP.NET page cycle. I want to fire event when page is going to be closed or redirected to other page. I try to use Page_Unload() but it is going to be fire when page display and in any button click event it is firing Page_Unload(). I want only to fire event when page is going to be redirected or close. Then I have tried to use the Javascript function window.onunload(). Again, same problem: it is firing when the first page displays. Is there any way to solve this?
Look into Jquery's .beforeunload property. Here is an example:
$(window).bind('beforeunload', function(){ return 'Click OK to exit'; });
Please note, beforeunload canot prevent a page from unloading or redirect it to another page for obvious reasons; it would be too easy to abuse. Also if you just want to run a function before unloading, try the following:
$(window).unload(function(){ alert('Bye.'); });
Finally, don't forget to referrence jQuery in your head tag by using:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
The above gets you the latest version from the internet and saves you the trouble to download it, and of course you can do so optionally, but I am just trying to get your thing to work asap.
Oh, I also found an example for you. Click here to see a page that calls a function before it closes. Hope this helps bud.

Categories

Resources