问题
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
回答1:
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.
回答2:
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/
回答3:
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.
回答4:
Have you bundled jQuery in Selenium jar? If not, you can't use that syntax.
回答5:
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/
回答6:
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
来源:https://stackoverflow.com/questions/4185640/get-element-with-jquery-and-selenium-ide-1-0-8