<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.
--