javascript - Htmlunit : How to get page updated after ajax dom manipulation -


using htmlunit 2.15, trying scrape third party website. therein textbox, onblur calls javascript function, adds option select box on same page.

with htmlunit, able fire onblur event, how handle "changed" page, has newly added option element?

code snippet:

final htmlpage page = webclient.getpage(myurl);  htmlselect selectdropdown = (htmlselect)page.getelementbyname(selectname); list<htmloption> options = clickthis.getoptions(); // returns 4 options   htmltextinput mytextbox = page.getelementbyname(textboxname); mytextbox.setvalueattribute("mytext"); mytextbox.fireevent(event.type_blur); 

// how "updated" page? should have 5 options

you need wait until javascript has changed page. experience may take while. if calling of server part of it.

so far attempt poll page until changed way expect to.

here method waits given text appear on page;

   private static final int ajax_max_tries_seconds = 30;    /**      * waits until given 'text' appeared or throws      * waitingforajaxtimeoutexception if 'text' not appear before timeout.      * @param page      * @param text text indicates ajax has finished updating page      * @param waitinglogmessage text log-output. should indicate in code are, , waiting      * @throws waitingforajaxtimeoutexception      */     public static void waitforajaxcallwaituntiltextappears(//             @nonnull final htmlpage page, //             @nonnull final string text,//             @nonnull final string waitinglogmessage) throws waitingforajaxtimeoutexception {         logger.debug("_5fd3fc9247_ waiting ajax call complete ... [" + waitinglogmessage + "]");         final stringbuilder waitingdots = new stringbuilder("   ");         (int = 0; < ajax_max_tries_seconds; i++) {              if (page.astext().contains(text)) {                 waitingdots.append(" ajax has finished ['").append(text).append("' appeared]");                 logger.debug("_8cd5a34faf_ " + waitingdots);                 return;             }             waitingdots.append('.');             wait(page);         }         logger.debug("_de5091bc9e_ "                 + waitingdots.append(" ajax timeout ['").append(text).append("' appeared not]").tostring());         logger.debug("_f1030addf1_ page source:\n" + page.asxml());         throw new waitingforajaxtimeoutexception();     } 

also make sure javascript enabled. (which default):

webclient.getoptions().setjavascriptenabled(true); 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -