admin管理员组

文章数量:1221428

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?

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?

Share Improve this question asked Jun 16, 2009 at 2:54 Matt SheppardMatt Sheppard 118k46 gold badges113 silver badges134 bronze badges 2
  • How are you running this script? Are you doing this from the Selenium IDE, or exporting it to another language (C#, Java, etc..) – Josh Commented Jun 16, 2009 at 3:13
  • From Java - I had assumed it didn't matter as it all came down to the same JavaScript – Matt Sheppard Commented Jun 16, 2009 at 4:07
Add a comment  | 

3 Answers 3

Reset to default 8

An more straightforward way to do it is:

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

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.

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.

本文标签: javascriptUsing selenium39s waitForCondition with an xpathStack Overflow