Support Questions

Find answers, ask questions, and share your expertise

jQuery Executing Too Early in Selenium WebDriver

i've a jQuery menu which expands upon mouseover. All sub-menu items can be clicked on and a few sub-menu items have similarly sub-items Selenium whilst hovered over. I want to briefly disable the clicking occasion of one of the mid-stage menu gadgets in order that clicking on it'll not fireplace its click event however will open the next sub-stage (i have attempted simulating hover over with the Selenium actions class however it does not work). i have the following code:

((IJavaScriptExecutor)driver).ExecuteScript("$('a:contains(Parent Menu Item)').click();"); ((IJavaScriptExecutor)driver).ExecuteScript("$('a:contains(Child Menu Item)').off('click');");

the first line works: the menu expands and the child element is visible. the second line has no impact. How can i get the jQuery to run within the order it seems in my code?

1 REPLY 1

New Contributor

You shouldn't. JQuery selectors offer most of what CSS 2 and CSS 3 selectors do, plus something more, but you can probably live without it. If you know JQuery selectors, you already know CSS selectors.

Use CSS selectors where you can and use XPath expressions where it's not enough (they're stronger). I doubt you'll find many real usages where these two aren't enough (and then, the usual approach is to get what you can and iterate over the collection, filtering the results manually).


That said, you could possibly force WebDriver to accept JQuery selectors, too:

If you only want to support one or two browsers, the easiest way might be to write a simple addon to that browser which would inject JQuery to every page if it's not already present. You'd then force this addon to be used by the browser you're using.

If you want to support all of the browsers, that solution quickly becomes a burden and the best you could do is to write a decorator for WebDriver that would try to inject JQuery into the page before any findElements() and/or executeScript() call, if it's not already present.

See this question to get an idea about injecting JQuery.

Take a Tour of the Community
Don't have an account?
Your experience may be limited. Sign in to explore more.