使用jquery和selenium IDE 1.0.8获取元素

发布于 2024-10-02 04:00:23 字数 818 浏览 0 评论 0原文

我正在尝试使用 jquery 和 Selenium IDe 1.0.8 获取元素。

<td>storeValue</td>
<td>$('#result').find('img').filter('[alt=&quot;NameOfPhoto&quot;]').eq(0)</td>
<td></td>

在日志中,我得到

[error] Element $('#result').find('img').filter('[alt="NameOfPhoto"]').eq(0) not found 

当我将此命令放入 firebug 中时,我得到此元素:/

为什么它不起作用?

编辑: 或者,例如,您可以给我代码如何在 stackoverflow 主页上获取带有 JAVA 标签的第一个对象的 id。

TAG:

<a rel="tag" title="show questions tagged 'java'" class="post-tag" href="/questions/tagged/java">java</a>

的示例结果

<div id="question-summary-4303985" class="question-summary narrow">

是:

question-summary-4303985

I'm trying to get element with jquery and Selenium IDe 1.0.8.

<td>storeValue</td>
<td>$('#result').find('img').filter('[alt="NameOfPhoto"]').eq(0)</td>
<td></td>

And in log I get

[error] Element $('#result').find('img').filter('[alt="NameOfPhoto"]').eq(0) not found 

When I put this command in firebug I get this element :/

Why it doesn't work ?

EDIT:
Alternatively for example you can give me code how to get id of first object whith JAVA tag at main page of stackoverflow.

TAG:

<a rel="tag" title="show questions tagged 'java'" class="post-tag" href="/questions/tagged/java">java</a>

and the example result from :

<div id="question-summary-4303985" class="question-summary narrow">

is:

question-summary-4303985

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

终止放荡 2024-10-09 04:00:23

根据其他帖子,我尝试了以下方法并且成功了。

将以下代码添加到 user-extensions.js:

function jQuery (selector)
{
    return selenium.browserbot.getUserWindow().jQuery(selector);
}

然后,您可以使用关键字 jQuery 而不是 $ 来访问任何 jQuery 函数。只需确保您正在测试的页面引用了 jQuery js 文件。

Based on the other posts I tried the following and it worked.

Add the code below to user-extensions.js:

function jQuery (selector)
{
    return selenium.browserbot.getUserWindow().jQuery(selector);
}

You can then access any jQuery function by using the keyword jQuery instead of the $. Just make sure that the page you are testing is referencing the jQuery js file.

不气馁 2024-10-09 04:00:23

要在 Selenium IDE 中使用 jQuery,其位置如下。 (确保已在页面中加载 jQuery)

this.page().getCurrentWindow().wrappedJSObject.jQuery()

您可以将函数位置存储在 Selenium 变量中。

<tr>
    <td>store</td>
    <td>this.page().getCurrentWindow().wrappedJSObject.jQuery</td>
    <td>jq</td>
</tr>

然后您可以在测试中使用它们,例如:

<tr>
    <td>assertEval</td>
    <td>${jq}('div').attr('foo')</td>
    <td>bar</td>
</tr>

上面使用 jQuery 匹配


编辑:或者,您可以通过以下方式访问它:

this.browserbot.getUserWindow().jQuery
selenium.browserbot.getUserWindow().jQuery

替代来源: http://cssgreut.wordpress.com/2010/12/20/run-selenium-ide-tests-with-jquery-selectors/

To use jQuery with Selenium IDE, it's location is below. (Be sure to have loaded jQuery in your page)

this.page().getCurrentWindow().wrappedJSObject.jQuery()

You can store the function location in a Selenium variable.

<tr>
    <td>store</td>
    <td>this.page().getCurrentWindow().wrappedJSObject.jQuery</td>
    <td>jq</td>
</tr>

Then you can use them in your Tests like:

<tr>
    <td>assertEval</td>
    <td>${jq}('div').attr('foo')</td>
    <td>bar</td>
</tr>

Above matches <div foo='bar' /> using jQuery.


Edit: Alternatively you could access it by:

this.browserbot.getUserWindow().jQuery
selenium.browserbot.getUserWindow().jQuery

source of alternate: http://cssgreut.wordpress.com/2010/12/20/run-selenium-ide-tests-with-jquery-selectors/

请恋爱 2024-10-09 04:00:23

尝试使用 jQuery 而不是 $()。美元符号在 selenium 中具有不同的含义。

编辑

据我所知,您不能使用 jQuery 来选择元素。谷歌搜索一无所获。只需使用 Xpath 即可。

try using jQuery instead of $(). The dollar sign has a different meaning in selenium.

EDIT

As far as I can tell you can't use jQuery to select elements. Google searches turned up nothing. Just use Xpath.

月竹挽风 2024-10-09 04:00:23

您是否将 jQuery 捆绑到 Selenium jar 中?如果不是,则无法使用该语法。

Have you bundled jQuery in Selenium jar? If not, you can't use that syntax.

記憶穿過時間隧道 2024-10-09 04:00:23

使用 JavaScript 存储当前日期并使用 ${} 将其回显到 Selenium 日志控制台的示例:

<tr>
    <td>store</td>
    <td>javascript{new Date}</td>
    <td>foo</td>
</tr>
<tr>
    <td>echo</td>
    <td>${foo}</td>
    <td></td>
</tr>

如您所见,将存储与 JavaScript 表达式的结果一起使用,添加 javascript{} 围绕表达式。

您的示例使用 javascript{storedVars['bar']} 输出存储的变量。

<tr>
    <td>store</td>
    <td>javascrip{jQuery('#result').find('img').filter('[alt="NameOfPhoto"]').eq(0)}</td>
    <td>bar</td>
</tr>
<tr>
    <td>open</td>
    <td>javascript{storedVars['bar']}</td>
    <td></td>
</tr>

Selenium 中的 JavaScript

使用javascript{alert('hello')},您可以在值字段中运行 JavaScript/jQuery。

Selenium 还有一个扩展来显示存储了哪些变量: http://reallysimplethings.wordpress.com/2010/09/28/the-stored-variables-viewer-plugin-for-selenium-ide-v1-3-已发布/

Example of storing the current date with JavaScript and echo it into the Selenium log console by using ${}:

<tr>
    <td>store</td>
    <td>javascript{new Date}</td>
    <td>foo</td>
</tr>
<tr>
    <td>echo</td>
    <td>${foo}</td>
    <td></td>
</tr>

As you can see to use store with an result of an JavaScript expression add javascript{} arround the expression.

Your example using javascript{storedVars['bar']} to output the stored variable.

<tr>
    <td>store</td>
    <td>javascrip{jQuery('#result').find('img').filter('[alt="NameOfPhoto"]').eq(0)}</td>
    <td>bar</td>
</tr>
<tr>
    <td>open</td>
    <td>javascript{storedVars['bar']}</td>
    <td></td>
</tr>

JavaScript in Selenium:

With javascript{alert('hello')} you can run JavaScript/jQuery in the value fields.

There is also an extension for Selenium to show which vars are stored: http://reallysimplethings.wordpress.com/2010/09/28/the-stored-variables-viewer-plugin-for-selenium-ide-v1-3-released/

戒ㄋ 2024-10-09 04:00:23

尝试一下德国拉姆的博客中的说明

首先,下载 Sizzle,它是 jQuery 的选择器引擎,并将 sizzle.js 解压到一个方便的位置。
其次,创建空的 user-extensions.js 文件。顺便说一句,名称可以是您想要的任何名称。
将其添加到 user-extensions.js

PageBot.prototype.locateElementBySizzle = function(locator, inDocument) {
  var results = [];
  window.Sizzle(locator, inDocument, results);
  return results.length > 0 ? results[0] : null;
}

第三,进入Selenium IDE,选项->选项...并将 sizzle.js 和 user-extensions.js 添加到“Selenium Core 扩展”。
重新启动 Selenium IDE(只需关闭它的所有实例并再次打开),现在您可以在任何需要“locator”的地方使用 sizzle=(locator)

您的选择器将如下所示:

#result img[alt="NameOfPhoto "]:eq(0)

JQuery 选择器主要来自 CSS 选择器,因此您也可以在 selenium 中使用它们

css=cssSelector

Try this instructions from German Rumm's blog

First, download Sizzle, which is a selector engine for jQuery and unpack sizzle.js to a convenient location.
Second, create empty user-extensions.js file. Name can be whatever you want by the way.
Add this to user-extensions.js

PageBot.prototype.locateElementBySizzle = function(locator, inDocument) {
  var results = [];
  window.Sizzle(locator, inDocument, results);
  return results.length > 0 ? results[0] : null;
}

Third, go to Selenium IDE, Options -> Options… and add sizzle.js and user-extensions.js to “Selenium Core extensions”.
Restart Selenium IDE (just close all instances of it and open it again), and now you can use sizzle=(locator) everywhere, where “locator” is needed

Your selector will looklike:

#result img[alt="NameOfPhoto"]:eq(0)

JQuery selectors mostly comes from CSS selectors, so you can use them also in selenium

css=cssSelector

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文