使用JQuery选择和识别元素以在Selenium 2 Java API中使用它 [英] Selecting and Identifying element with JQuery to use it in Selenium 2 Java API

查看:89
本文介绍了使用JQuery选择和识别元素以在Selenium 2 Java API中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况如此:


  • 我使用Selenium 2的Java API来打开和控制firefox浏览器实例

  • 我通过JavaScript执行将jQuery脚本加载到页面

  • 然后我使用jQuery表达式选择元素并遍历DOM树

现在是我的问题,我能以某种方式找到每个找到的元素的唯一标识符吗?我的目标是通过使用Xpath或CSS选择器与Selenium获得相同的元素。因此,如果我可以为jQuery中的元素生成一个明确的选择器,那将是最直接的。其他想法也是受欢迎的。

Now is my question, can i somehow find a unique identifier for each of the found elements? My goal is to get the same element with Selenium by using an Xpath or CSS selector. So it would be most straighforward if i could generate an unambiguous selector for the elements in jQuery. Other ideas are welcome too.

我需要一种自动方法来识别jQuery中的元素,这些元素可以转换为Selenium元素/定位器。

I need an automatic approach for identifying elements in jQuery, which can be "converted" to Selenium elements / locators.

/ edit

为了更清楚:

如果我选择了jQuery中的一个元素:

If i have selected an element in jQuery:

webDriver.executeScript("var element = $('#myDiv input.test')");

现在,我想要这样的东西:

Now, I want something like this:

WebElement webElement = webDriver.executeScript("return element");

这可能吗?

推荐答案

我找到了解决方案,非常简单:

I found the solution, which is quite easy:

String jQuerySelector = "'#myDiv input.test'";
RenderedWebElement webElement = (RenderedWebElement) ((JavascriptExecutor) webDriver).executeScript("return $(" + jQuerySelector+ ").get(0);");

使用在Selenium中常用的jQuery中的元素也是如此:

Working with an element in jQuery which was previosly selected in Selenium works too:

String jQuerySelector = "arguments[0]";
((JavascriptExecutor) webDriver).executeScript("return $(" + jQuerySelector+ ").doSomethingInJquery();", webElement);

这篇关于使用JQuery选择和识别元素以在Selenium 2 Java API中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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