1

I'm running into problems when executing Selenium 2.18 WebDriver tests against an Oracle SSXA-based site, which translates to tons of popups, Ajax-loaded content and iframes. For a given page, based on manual observation, the page is initially loaded with an empty sslw_doc_content_id span (no text). About a second later, the span still exists and contains text.

To check that this page has loaded, I'm using a WebDriverWait with a Predicate that checks that the sslw_doc_content_id span has non-empty text:

new Predicate<WebDriver>() {
    @Override
    public boolean apply(final WebDriver input) {
        return StringUtils.isNotEmpty(input.findElement(By.id("sslw_doc_content_id")).getText());
    }
}

Somehow, WebDriver always finds the WebElement but always returns an empty string when calling WebElement.getText(). And so this predicate always evaluates to false.

Inspecting the page with Chrome or Firefox shows that the element exists and does have text. When debugging the predicate, I've observed that input.getPageSource() contains the span with no text on its first invocation, but that input.getPageSource() contains the span with some text on its second invocation (after the page has been ajax-refreshed).

Why doesn't WebDriver consider the refreshed page source on the second invocation?

Thanks!

2 Answers 2

0

You can try loop:

int seconds = 0;
for (int seconds =0; seconds<30; seconds++) {
      if (apply(driver)){
        break;
        }
      Thread.sleep(1000);         
}

The above loop will check every second if text is there and do it for 30 seconds. If it finds it, it will escape the loop. You can still add one more check after:

if (!apply(driver)){
  throw new RuntimeException("Expected text still not there");
}

Or another approach, which I am using: Whenewer I have page to load for long time, I select element which is loaded among the last on the page and check its presence the same way:

for (int seconds =0; seconds<30; seconds++){
 try{
 driver.findElement(By.id("lastelementonpage"));
}catch (Exception e){
 Thread.sleep(1000);
}
}
2
  • Actually I'm already using a WebDriverWait, which does the looping and sleeping thing for me. Thanks anyway :)
    – Spiff
    Feb 9, 2012 at 18:09
  • hmm, then its awkward why its not doing anything. Another plain guess. Try to put Thread.sleep() right after you load page. Its really "dirty" approach, but could do the job Feb 10, 2012 at 6:30
0

It turns out that WebDriver does not allow access to hidden elements, and this span was hidden.

http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_Why_is_it_not_possible_to_interact_with_hidden_elements?

I've resorted to using the JavaScript-based solution described in the above link.

1
  • Ahh! How obvious :) I am glad you solved it. I was completly clueless :) Feb 10, 2012 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.