JQuery Onclick event handler not invoked when element clicked with WebDriver

2,037 views
Skip to first unread message

Erich

unread,
Jun 25, 2012, 6:14:58 PM6/25/12
to seleniu...@googlegroups.com
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.

Here is the HTML and Javascript on the page with the element:
HTML:
<fieldset class="onecolumn">
 
<div class="row centered">
 
<a href="/profile/List" id="cancelButton">Cancel</a>
 &nbsp;&nbsp;
 
<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>





JavaScript:
$(document).ready(function () {
 $
('#cancelButton').click(confirmCancel);
 
});





In a separate script only file, the confirmCancel method is defined:

var confirmCancel = function () {
 
if ($(this).isFormDirty() == true) {
 
return confirm('Any unsaved changes will be lost. Proceed?');
 
} else {
 
return true;
 
}
};




This is all tied together using the jQuery library 1.4.4 handler function.

When I manually navigate through the pages, everything works as expected. However, when I use WebDriver to click the Cancel button described above, it ignores the handler and goes straight to the URL defined in the href. I have put debug operations in various parts of the Javascript to confirm that the event-handling assignment code gets called, and that the confirmCancel method is not getting called at all.

In another odd behavior, when I get into FF or IE and break-on-action, WebDriver triggers the event handlers as expected. If I pause my Selenium script (C#) just before clicking cancel, tell the browser to break on the next JavaScript line, and resume the script, the Click Handler is invoked as expected.

As mentioned above, this happens on both IE 9 and FF 9, using Selenium 2.22.0.

Thanks!

Peter Gale

unread,
Jun 25, 2012, 6:56:42 PM6/25/12
to seleniu...@googlegroups.com
Erich

I sometimes have problems where my Selenium Webdriver code seems to fight with events and Javascript in web pages.

And I'm not really a html/javscript developer myself, but it strikes me as a bit odd that the link element has both an href and an onclick event.

Even it if is allowed in the html standards (which I doubt), it seems bad practice to try to have two things like this both triggered when the link is clicked, so it's perhaps little surprise that there are problems interacting with the page.

Maybe there is something in Selenium WebDriver which (perhaps rightly) forces the href to be followed first and foremost, whereas browsers often do their best to handle dubious code in web pages. That's not something Selenium can afford to do, for which it is often seen to be at fault.

If the developer wants the onclick event to always occur before the URL, for whatever type of user is interacting with the page, I'd suggest using a different type of html element (perhaps a button?) without an embedded href and putting a redirection to the new URL at the end of the code behind the onclick action rather than in a href attribute.

Peter


Date: Mon, 25 Jun 2012 15:14:58 -0700
From: eri...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] JQuery Onclick event handler not invoked when element clicked with WebDriver
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/4L9nuNrXD54J.
For more options, visit https://groups.google.com/groups/opt_out.

Mark Collin

unread,
Jun 26, 2012, 5:37:07 AM6/26/12
to seleniu...@googlegroups.com

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.

--

Erich

unread,
Jun 26, 2012, 9:49:50 AM6/26/12
to seleniu...@googlegroups.com
Thanks Mark,

That was my thought as well. I have certainly run into plenty of issues where WebDriver/Selenium is happy to cruise forward before the page is fully loaded. I put some pieces into place in the page JavaScript to indicate that the event-handlers have been wired up, and that doesn't seem to be the issue. But I do think the complexity of connecting multiple functions may be involved here, so I will continue to pursue that angle. These types of problems do seem to smell of timing problems somewhere in the system.

Thanks again,

-erich

Erich

unread,
Jun 26, 2012, 9:58:59 AM6/26/12
to seleniu...@googlegroups.com
Thanks Peter,

That is not an angle I had quite explored. It does seem like some conflict between how the web app/browser deals with events and links vs. how WebDriver forces events and links (realizing, of course, that WebDriver is supposed to externally trigger the browser in the exact same way a user would).

It is refreshing to see that I am not the only one dealing with these issues. I will try inserting some javascript waits for everything to get wired up.

My understanding is that browser implementations are okay handling the event before the href, but I would not be surprised if some issue confuses either WebDriver or jQuery into not realizing the event handler is there.


Reply all
Reply to author
Forward
0 new messages