Skip to main content
Software View

Main navigation

  • Home
  • Trainer Profile
  • Blog
  • Contact
User account menu
  • Log in

Breadcrumb

  1. Home
  2. Blog

Java Programmatic Browser

By Kamal Wickramanayake, 19 July, 2010

At times you need to visit web sites, login, navigate through pages, select portions of HTML, click on links, check for the existence of a form, submit the form,.... and do all these things programmatically. So you need a programmable web browser that can execute and have a cup of tea while it will do the job.

Java programmatic browser

Java SE API has the HTMLEditorKit that you can use to parse HTML pages. I have used it once. But it's very limited in capabilities. It's for parsing, but not to implement simple or complex navigation scenarios.

I have been a lover of httpunit to do things of this nature. I have used it to navigate through pages and fetch content in really complex scenarios. It's so powerful that it even knows how to execute JavaScript. httpunit is a JUnit extension. So you can use it to write JUnit test cases for your project. Let's not worry about test cases. Let's look at how to use it for some simple navigation and parsing the response pages. Look at the code sample given below. Follow the comments to understand what each important line does.

package org.swview.mybrowser;

import java.io.IOException;

import org.xml.sax.SAXException;

import com.meterware.httpunit.HttpUnitOptions;
import com.meterware.httpunit.TableCell;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;

public class ProgrammaticBrowser {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // Don't throw exceptions when JavaScript errors occur
        HttpUnitOptions.setExceptionsThrownOnScriptError(false);
       
        // Here's the browser
        WebConversation wc = new WebConversation();

        try {
            // Fetch a page
            WebResponse response = wc.getResponse("http://www.swview.org/");
           
            // Get the link with the text "Contact"
            WebLink link = response.getLinkWith("Contact");
           
            // Click on the link and get the next response
            response = link.click();
           
            // Get all tables
            WebTable[] tables = response.getTables();
           
            // Get the first table
            WebTable firstTable = tables[0];
           
            // Get the cell at first row, second column
            TableCell emailCell = firstTable.getTableCell(0, 1);
           
            // Print the content as text
            System.out.println(emailCell.getText());
           
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }        
    }
}

Given that httpunit.jar and the other jar file dependencies of httpunit are added to the class path, you can execute the above like this (after compilation):

java org.swview.mybrowser.ProgrammaticBrowser

You can even manipulate JavaScript generated content. For example, you can click on a JavaScript generated button. You can even navigate to another browser window that may pop up when you click on a link. I love it!

See below to download this simple application as an Eclipse project with httpunit libraries packed inside.

Java

Highlights

  • O'Reilly Book "97 Things Every Software Architect Should Know" Accepts A Write Up From Kamal
  • "Service Oriented Architecture - Making IT Infrastructure Speaks Business" - Presentation At The ISACA 4th Annual Conference
  • The Second Bacth Of ICTA Nanasala e-Society Members Receives Trainings On HTML/CSS and GIMP
  • GIMP Training For ICTA Nanasala Project
  • Agile Processes Training For PPSL (Pvt) Ltd
  • Computer Society of Sri Lanka (CSSL) - Talk on "Introduction to IT Governance and Enterprise Architecture"
  • Motorola Sends A Second Batch Through Software Patterns Training
  • Kamal To Act As The Marketing Director - ISACA Sri Lanka Chapter
  • ISACA Sri Lanka Chapter Invites Kamal To Join As A Board Member
  • Epic Lanka Technologies (Pvt) Ltd Receives Java SE And Java EE Trainings From Software View
  • Patterns Training For PPSL (Pvt) Ltd
  • ISACA Members Day Presentation On "Introduction To IT Governance And Enterprise Architecture"
  • Opening Lecture On Information Technology For SLIDA Master Of Public Management Course Delivered By Kamal
  • Customized Java Enterprise Edition Training For SLIDA
  • No One To Beat Software View - Epic Lanka Technologies (Pvt) Ltd
  • Motorola Receives Software Patterns Training From Software View
  • Custom Java Enterprise Edition Training for ICTA/SLIDA - Only from Software View!
  • Java EE 5, JavaServer Faces, Hibernate And Spring For PPSL (Pvt) Ltd
  • "Brain Trust" For Linux Journal Weekly Newsletter From Kamal
  • Java Platform, Enterprise Edition 5 Training At The CEIT, University Of Peradeniya
  • Another Group Of Around 100 Sri Lanka Telecom Engineers And Managers Were Service Oriented!
  • Java Platform, Enterprise Edition 5 Training Will Be Held At The CEIT, University Of Peradeniya
  • Service Oriented Architecture: Another Two Sessions Conducted at SLT
  • Photos of IET Monthly Forum at the Peradeniya University
RSS feed
Copyright © 2007 - 2023 Software View