Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java SE: Open Web Page and Click a Button

Tags:

java


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.

like image 433
Serge Vinogradoff Avatar asked May 25 '13 03:05

Serge Vinogradoff


People also ask

How can I programmatically click a button on a Web page?

You can't 'programmatically' click a button with Java alone, which is why we use JavaScript. If you want to get into controlling the browser such as clicking buttons and filling text fields you would have to use an automation tool. An automation tool that uses Java is Selenium.

How do I run a Java program by clicking a button in HTML?

Maybe you were refering to showing the applet after the user clicks the button. In that case, the solution may be to add your <object> to a <div> that has a css-property of visibility:hidden; . Then create a HTML-button element that accesses the <DIV> element and changes its visibility property, e.g.

How do you code a button in Java?

To create a button, simply instantiate the JButton class in your Java code like so: JButton button = new JButton("Button"); Programmers can supply a string (or icon) to the constructor of JButton as an identifier on the screen.


1 Answers

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.
like image 179
acdcjunior Avatar answered Oct 19 '22 22:10

acdcjunior