11


I have a Java 7 program (using WebStart technology, for Windows 7/8 computers only).

I need to add a function so that my program clicks a button on a page with known URL (https).

Some people suggest WebKit SWT, but I went to their site and they say that the project was discontinued. (http://www.genuitec.com/about/labs.html)

Other people say that JxBrowser is the only option but it looks like it's over $1,300 which is crazy. (http://www.teamdev.com/jxbrowser/onlinedemo/)

I'm looking for something simple, free, lightweight, and able to open HTTPS link, parse HTML, access a button through DOM and click it. Perhaps some JavaScript too, in case there are JS handlers.

Thanks for your help.

1
  • JavaFx ships with java 7 and has a webview object which is basically a browser. Commented May 25, 2013 at 5:53

2 Answers 2

13

You may be looking for HtmlUnit -- a "GUI-Less browser for Java programs".

Here's a sample code that opens google.com, searches for "htmlunit" using the form and prints the number of results.

import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;

public class HtmlUnitFormExample {
    public static void main(String[] args) throws Exception {
        WebClient webClient = new WebClient();
        HtmlPage page = webClient.getPage("http://www.google.com");

        HtmlInput searchBox = page.getElementByName("q");
        searchBox.setValueAttribute("htmlunit");

        HtmlSubmitInput googleSearchSubmitButton = 
                          page.getElementByName("btnG"); // sometimes it's "btnK"
        page=googleSearchSubmitButton.click();

        HtmlDivision resultStatsDiv =
                                page.getFirstByXPath("//div[@id='resultStats']");

        System.out.println(resultStatsDiv.asText()); // About 309,000 results
        webClient.closeAllWindows();
    }
}

Other options are:

  • Selenium: Will open a browser like Firefox and operate it.
  • Watij: Also will open a browser, but in its own window.
  • Jsoup: Good parser. No JavaScript, though.
Sign up to request clarification or add additional context in comments.

1 Comment

HtmlUnit does not open any browser, it emulates one. It is full Java code.
0

Your question is kind of difficult to understand what you want. If you have a webstart app and want to open a link in the browser, you can use the java.awt.Desktop.getDesktop().browse(URI) method.

public void openLinkInBrowser(ActionEvent event){

    try {
        URI uri = new URI(WEB_ADDRESS);
        java.awt.Desktop.getDesktop().browse(uri);

    } catch (URISyntaxException | IOException e) {
        //System.out.println("THROW::: make sure we handle browser error");
        e.printStackTrace();
    }

}

2 Comments

what about clicking the button? does it allow that?
Yes, the method called using the ActionEvent that's passed to it could easily be triggered by a button.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.