views:

3637

answers:

3

Selenium has some nice support for finding elements in a page via xpath

selenium.isElementPresent("//textarea")

and in ajax pages you can use waitForCondition to wait on a page until something appears

selenium.waitForCondition("some_javascript_boolean_test_as_a_string", "5000")

My difficulty is, I can't seem to get into a position to use the xpath support for the boolean test. document.getElementById seems to work fine, but selenium.isElementPresent doesn't.

Is there any easy way of accessing selenium's xpath element finding abilities from within waitForCondition's first parameter?

+2  A: 

If you want to select an element by id, the simplest thing is to use straight selenese:

 isElementPresent("textarea")

If you insist on using xpath you may have to use

isElementPresent("//*[@id='textarea']")

or

isElementPresent("//id('textarea')")

(The id function may not be cross-browser compatible)

If this does not help your html document may also have structural issues that is causing this to happen. Run an html validator like html validator to check for nesting problems and similar on your page.

krosenvold
+3  A: 

You could do something like this:

selenium.waitForCondition("var x = selenium.browserbot.findElementOrNull('elementIdOrXPathExpression'); x != null && x.style.display == 'none';", "5000");

The waitForCondition just evaluates the last js expression result as a boolean, thereby allowing you to write complex tests (as shown here). For example, we're also using this technique to test whether jQuery AJAX has finished executing.

The only drawback is you're also now relying on the browserbot and the lower level core js API.

ixi
+4  A: 

An more straightforward way to do it is - selenium.waitForCondition("selenium.isElementPresent(\"xpath=//*[@id='element_id_here']/ul\");", time_out_here);

pearl