最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using selenium's waitForCondition with an xpath - Stack Overflow

programmeradmin3浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论