I would like to use Selenium to click on the tab of a web where the tab was created dynamically using JQuery. There is one problem, since it was created dynamically and the tab got no ID tied to it (only class-ID provided), so I am running out of clue to click on it using Selenium.
After googling for 2 weeks, I found out that it could be done using JQuery by injecting JQuery into Selenium and repackaging it so that it support JQuery API. But the problem now is I don't know how to trigger JQuery script in Selenium?
Is there any resources out there or guideline on setting up JQuery in Selenium? How am I going to execute JQuery in Selenium?
The known javascript library, called jQuery, provides a bunch of methods that can be performed over any web element and, of course, since Selenium is able to run javascript code, you can run jQuery methods on them using WebDriver.
Selenium is an open-source automation testing tool that supports a number of scripting languages like C#, Java, Perl, Ruby, JavaScript, etc.
jQuery code is implemented as part of JavaScript scripts. To add jQuery and JavaScript to your web pages, first add a <script> tag that loads the jQuery library, and then add your own <script> tags with your custom code.
You can try using my selenium lib at github.
It handles almost the entire jquery API minus the functions that use/require handler passing:
HtmlUnitDriver drv = new HtmlUnitDriver(BrowserVersion.FIREFOX_3_6);
drv.setJavascriptEnabled(true);
try {
jQueryFactory jq = new jQueryFactory();
jq.setJs(drv);
drv.get("http://google.com");
jq.query("[name=q]").val("SeleniumJQuery").parents("form:first").submit();
String results = jq.queryUntil("#resultStats:contains(results)").text();
System.out.println(results.split(" ")[1] + " results found!");
} finally {
drv.close();
}
Since you said that you didn't have an ID but a class:
(only class-ID provided)
...a better answer is likely to use the CSS locator strategy which is already baked-in to Selenium where you can select an element based on a css class or simply by using CSS selector logic (for at least css2 and css3)
So to select an element (div, span whatever) that has a specific class you can simply use this for the Selenium locator:
css=.class-ID
You can even use more complicated selectors that are similar to those available in JQuery such as:
css=#myDiv .class-ID
This will search for the element with a css style of class-ID
within the element with an ID = myDiv
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With