Tameroski
Tameroski

Reputation: 98

Don't wait for click() response in HtmlUnit

On one of the pages i parse with HtmlUnit, i need to click() a link which loads a page that is very long to load.

The thing is i don't need to wait for the response; i just have to click the link and go on doing things with my WebClient object.

HtmlElement linkIdontCareAboutButHaveToClick = wc.checkAndGetElement("//div[@class='next']/a");

if (linkIdontCareAboutButHaveToClick != null)
{
    wc.click(lienValidationPropocom);
}

// do some more thing with wc without waiting
...

Is there a way to tell HtmlUnit to do some kind of asynchronous click() ?

Needless to say that everything has to be bound to the session.

Upvotes: 0

Views: 915

Answers (1)

buggzy
buggzy

Reputation: 11

I think you need to instantiate a new background thread to do the .click() so your main Event Dispatch Thread can continue without stopping.

First encapsulate the work to be done in the background in it's own class. (you may have to pass your wc element in as a parameter depending on it's scope):

class backgroundWork extends Thread {
    public void run() {
        wc.click(lienValidationPropocom);
    }
}

Then you can execute it from your main thread:

if (linkIdontCareAboutButHaveToClick != null)
{
    Thread thread = new backgroundWork();
    thread.start();
}

Upvotes: 1

Related Questions