Java se: open the web page and click the button

I have a Java 7 program (using webstart technology, only applicable to Windows 7 / 8 computers)

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

Some people suggested using WebKit SWT, but I went to their website and they said the project had stopped ( http://www.genuitec.com/about/labs.html )

Others say jxbrowser is the only option, but it looks like it's over $1300. It's crazy ( http://www.teamdev.com/jxbrowser/onlinedemo/ )

I'm looking for something simple, free, lightweight, able to open HTTPS links, parse HTML, access buttons through Dom and click it Maybe some JavaScript in case there is a JS handler

Thanks for your help.

Solution

You may be looking for htmlunit - a "Gui less browser for Java programs"

This is to open Google COM, use the form to search "htmlunit" and print 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 search@R_744_2419@ = page.getElementByName("q");
        search@R_744_2419@.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: a browser like Firefox will open and operate. > Watij: a browser will also open, but in its own window. > Jsup: good parser But no JavaScript

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>