1

I'm using HtmlUnit [see Testing a website using C# ], and I bumped into a weird problem:
I'm trying to call a page, click a button and retrieve the subsequent page. It works fine, but sometimes it bumps out with ElementNotFoundException when I try setting the value attribute for a field in the retrieved page.

I tried adding a Sleep(1000), but it doesn't help...

Any idea?

Community
  • 1
  • 1
charlie
  • 77
  • 1
  • 8

2 Answers2

2

this is equal to when you click a button on web page using Web Browser, and because of slow internet connection it will not respond, so you need to reload the page again and so you must do the same in your code handle reloading page again when this exception thrown.

0

Try adding a catch of ElementNotFoundException and do a print of page.toXml() out to the console when it happens. Chances are you are not getting the page you think you are getting.

Rodney Gitzel
  • 2,652
  • 16
  • 23
  • I noticed that I'm getting this exception only when I have a weak internet connection. Once it is strong it works fine. It seems that HtmlUnit has some TimeOut, and when it expires he just returns a Null object.... – charlie Aug 18 '10 at 12:49
  • That could well be: http://htmlunit.sourceforge.net/apidocs/com/gargoylesoftware/htmlunit/WebClient.html#getTimeout%28%29 – Rodney Gitzel Aug 18 '10 at 19:36