java程序在浏览器中打开一个网页并将一些数据发布到打开的页面中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22292124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:46:10  来源:igfitidea点击:

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

javapostbrowser

提问by user1254261

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.

我陷入了一个问题。问题是我想编写一个 Java 代码,它在默认浏览器中打开一个网页并将我的数据发布到该打开的网页中。

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

请任何人都可以指导我这样做。我对此一无所知。

Your help is much appreciated. Thanks in advance

非常感谢您的帮助。提前致谢

采纳答案by AbhinavRanjan

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

为此,您将需要一个网络驱动程序。看看

I am pasting some code from Getting started with Selenium

我正在粘贴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.

此示例显示了如何打开 google.com,然后键入一些搜索文本并单击搜索按钮。

回答by Priyatam Roy

This will open the default browser

这将打开默认浏览器

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

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

看看这个,看看如何在 url 中传递参数

回答by hienbt88

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

您可以编写一个带有表单的 HTML 文件自动提交到服务器并使用这些东西打开它:

File htmlFile = new File(url);
Desktop.getDesktop().browse(htmlFile.toURI());