<fieldset class="onecolumn">
Ā <div class="row centered">
Ā <a href="/profile/List" id="cancelButton">Cancel</a>
Ā
Ā <a href='/profile/Add' class="baseButton" id="next" onclick="$('#_master_RedirectAction').val('/profile/List'); $('#_master_MainForm').submit(); return false;; return false;" >Next</a>
Ā </div>
</fieldset>
$(document).ready(function () {
Ā $('#cancelButton').click(confirmCancel);
Ā });
var confirmCancel = function () {
Ā if ($(this).isFormDirty() == true) {
Ā return confirm('Any unsaved changes will be lost. Proceed?');
Ā } else {
Ā return true;
Ā }
};
I think you are suffering from the āWhen is the page loadedā problem.
Ā
It sounds like WebDriver thinks the page has loaded before jQuery has finished modifying the DOM and is interacting with elements before jQuery has managed to add an event handler.
Ā
I would add in an explicit check to ensure that JQuery has finished processing before letting your script go wild on the page, something like this:
Ā
public void waitUntilJQueryProcessingHasCompleted(int timeout) {
Ā Ā Ā new WebDriverWait(driver, timeout) {
Ā Ā Ā }.until(new ExpectedCondition<Boolean>() {
Ā Ā Ā Ā Ā Ā Ā @Override
Ā Ā Ā Ā Ā Ā Ā public Boolean apply(WebDriver driver) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā boolean jQueryActive = (Boolean) ((JavascriptExecutor) driver).executeScript("return jQuery.active == 0");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return jQueryActive;
Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā });
}
Ā
(The above isnāt tested so YMMV, if nothing else it should point you in the right direction)
From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Erich
Sent: 25 June 2012 23:15
To: seleniu...@googlegroups.com
Subject: [selenium-users] JQuery Onclick event handler not invoked when element clicked with WebDriver
Ā
I am having a problem automating a page on our web site. My ultimate goal is to get a handle on a Cancel-Confirm box, but it appears that when I use Selenium 2 to access a button, the attached (JQuery) event handler is being ignored.
--