JaMIE Wiki
Welcome to the Java Microsoft® Internet Explorer project.
It's goal is to provide an easy to use API for remote controlling instances of the Internet Explorer. As a bridge from the Java world to the Microsoft® COM realm we use com4j.
Basic Use Case
The most basic use case is the following:
package scratch;
import org.georgulbrich.jamie.InternetExplorer;
public class NewMain {
public static void main(String[] args) {
InternetExplorer ie = new InternetExplorer();
ie.navigate("http://www.google.de");
ie.quit();
}
}
It creates a new (visible) instance of IE and starts a navigation to the given URL. JaMIE returns from the navigation method only after the whole document, including all it's frames, has finished loading. Other frameworks lack reliability in this important aspect. Indeed, IE provides a busy flag and an event that (are supposed to) signalize the finalization of the document loading, but both are unreliable (fire to early in some cases). JaMIE does not rely on these two methods, but instead iterates through the document tree (frames) and waits for each frame document to finish loading.
Event Handling
Thanks to com4j, it is easy to capture events fired by the browser instance. In order to subscribe to the events, supply a handler to the constructor of the IE.
package scratch;
import org.georgulbrich.jamie.InternetExplorer;
public class NewMain {
public static void main(String[] args) {
InternetExplorer ie = new InternetExplorer(new MyHandler());
ie.navigate("http://www.google.de");
ie.quit();
}
}
The handler class has to extend the BrowserEvents class:
package scratch;
import com4j.Com4jObject;
import com4j.Holder;
import org.georgulbrich.jamie.BrowserEvents;
public class MyHandler extends BrowserEvents {
@Override
public void beforeNavigate2(Com4jObject pDisp, Object url, Object flags, Object targetFrameName, Object postData, Object headers, Holder<Boolean> cancel) {
System.out.println("Hey, I'm starting a navigation to " + url);
}
}
Check the API documentation for all event methods that you may override (all do nothing by default). A more detailed description can be found on the Microsoft Developer Network. Look through the events of the InternetExplorer object.
Accessing Elements
Navigating around is nice, but entering data, clicking buttons etc. is nicer. Here is a slightly more complex use case.
package scratch;
import org.georgulbrich.jamie.Document;
import org.georgulbrich.jamie.Element;
import org.georgulbrich.jamie.InternetExplorer;
public class NewMain {
public static void main(String[] args) {
InternetExplorer ie = new InternetExplorer();
ie.navigate("http://www.google.de");
Document document = ie.getDocument();
Element field = document.find("name=q");
field.setAttribute("value", "Jimi Hendrix");
Element button = document.find("name=btnG");
button.click();
//ie.quit();
}
}
First we navigate to Google, then we get hold of the current HTML document. With the find-method we first obtain the input field (identified by it's name) and fill it with some value. Then we find the "Search"-Button and click. The click, like navigation, has a wait included.
One more word concerning element selection: the find-method of the document takes a list of Strings (Java vararg-feature), each of these criteria is of the form "attribute=pattern". The find-method tries to find a HTML element such that for each criteria the elements attribute matches the pattern. Here, pattern may be an arbitrary regular expressin. For example
Element button = document.find("class=navigationClass", "id=guitarsolo.*");
gives an element of style class navigationClass and whose id starts with guitarsolo.
Have fun :)





