Matt Sheppard
Matt Sheppard

Reputation: 118033

Using selenium's waitForCondition with an xpath

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?

Upvotes: 6

Views: 15247

Answers (3)

pearl
pearl

Reputation: 217

An more straightforward way to do it is:

selenium.waitForCondition("selenium.isElementPresent(\"xpath=//*[@id='element_id_here']/ul\");", time_out_here);

Upvotes: 8

ixi
ixi

Reputation: 51

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.

Upvotes: 5

krosenvold
krosenvold

Reputation: 77201

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.

Upvotes: 3

Related Questions