如何在Selenium中使用JQuery? [英] How to use JQuery in Selenium?

查看:253
本文介绍了如何在Selenium中使用JQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Selenium来单击Web的选项卡,其中该选项卡是使用JQuery动态创建的.有一个问题,因为它是动态创建的,并且该选项卡没有绑定任何ID(仅提供了类ID),所以我不知道要使用Selenium单击它.

在谷歌搜索2周后,我发现可以通过将JQuery注入Selenium并重新包装使其支持JQuery API来使用JQuery来完成.但是现在的问题是我不知道如何在Selenium中触发JQuery脚本?

是否有任何资源或有关在Selenium中设置JQuery的指南?我将如何在Selenium中执行JQuery?

解决方案

您可以在 github 上尝试使用我的Selenium库. /p>

它处理几乎整个jquery API,减去使用/需要处理程序传递的函数:

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();
}

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?

解决方案

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();
}

这篇关于如何在Selenium中使用JQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆