How to Open the Default Webbrowser Using Java

How do you open web pages in Java?

Opening a web page with the default web browser is easy:

java.awt.Desktop.getDesktop().browse(theURI);

Embedding a browser is not so easy. JEditorPane has some HTML ability (if I remember my limited Swing-knowledge correctly), but it's very limited and not suiteable for a general-purpose browser.

java program to open a web page in browser and post some data into the opened page

You will need a web driver for this. Look at Selenium

I am pasting some code from Getting started with Selenium

package org.openqa.selenium.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class Example {
public static void main(String[] args) {
// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new HtmlUnitDriver();

// And now use this to visit Google
driver.get("http://www.google.com");

// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));

// Enter something to search for
element.sendKeys("Cheese!");

// Now submit the form. WebDriver will find the form for us from the element
element.submit();

// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());

driver.quit();
}
}

This example shows how you can open google.com and then type some search text and click the search button.



Related Topics



Leave a reply



Submit