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

15,627

Solution 1

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.

Solution 2

This will open the default browser

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

Have a look at this to see how to pass parameter in url

Solution 3

You can write a HTML file with form automatically submit to server and use this stuff to open it:

File htmlFile = new File(url);
Desktop.getDesktop().browse(htmlFile.toURI());
Share:
15,627
user1254261
Author by

user1254261

Updated on June 04, 2022

Comments

  • user1254261
    user1254261 almost 2 years

    I m stuck into a problem. The problem is that i want to write a java code which opens a web page in a default browser and post my data into that opened web page.

    Please could anyone guide me to do this. I dont have a clue in this.

    Your help is much appreciated. Thanks in advance

  • user1254261
    user1254261 about 10 years
    that code didnt work..it thows this exception... org.openqa.selenium.NoSuchElementException: Unable to locate element with name: q
  • user1254261
    user1254261 about 10 years
    i went through your example to pass parameter in url..but its not working. anyother idea please
  • Priyatam Roy
    Priyatam Roy about 10 years
  • user1254261
    user1254261 about 10 years
    while running that example from your link above throws this exception...java.net.ConnectException: Connection refused: connect
  • user1254261
    user1254261 about 10 years
    it worked..i got the response in the console..but it didnt open the page in the browser.
  • AbhinavRanjan
    AbhinavRanjan about 10 years
    You need to download the jar file from the first link and add it to your classpath.
  • AbhinavRanjan
    AbhinavRanjan about 10 years
    It would be better if you followed the instructions in the second link. This code was not supposed to work out of the box. This was to give an idea of what is possible if you follow this approach.
  • user1254261
    user1254261 about 10 years
    my need is to open a browser with the data which is sent through post method.
  • Gobliins
    Gobliins over 7 years
    any chance to get the response from the opened page after entering some data? stackoverflow.com/questions/40545880/…