如何使用Java程序在浏览器中操作文本字段

How to manipulate a Textfield in the Browser using a Java Programm?

本文关键字:操作 作文本 字段 浏览器 何使用 Java 程序      更新时间:2024-06-17

嗨,我的问题是一种自动化问题。我想访问一个由两个可编辑的文本字段和一些非重要元素组成的特定页面,将所有文本字段读取到Java程序中,该程序用于过滤关键字并生成字符串。然后,如果可能的话,我想把这个字符串发回其中一个文本字段,把我的字符串发到网站上。问题是我不拥有这个网站,我想访问的页面是密码安全的(我需要登录),我当然知道。此外,除了Java之外,我什么都不熟悉,否则我会用另一种语言编写过滤器。

首先,我尝试使用Java中的URL类读取源代码,在该代码中搜索两个文本字段的内容,然后进行过滤。但由于密码保护,我无法连接到页面。在那之后,我试图找到一个变通方法,我想写一个Mozilla Firefox插件,它通过"document.dococumentElement.inerHTML"获取源代码,然后将其提供给我的Java程序,或者将其写在一个临时文件中以供我的程序访问。但我也找不到命令。

因此,我需要一个解决方案来发送访问特定页面的密码,或者以另一种方式获取文本字段的内容,获取页面的源代码就可以了。最后,我需要一个平滑的方式来发布我的过滤字符串到网站上。

提前谢谢,我希望你能理解我的问题并找到解决方案。

public static void main(String[] args) {
    URL url;
    InputStream is = null;
    BufferedReader br;
    String line;
    String header;
    String textbox;
    String shopname;
    try {
        url = new URL("TestURL");
        is = url.openStream();  // throws an IOException
        br = new BufferedReader(new InputStreamReader(is));
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            if(line.contains("Gib hier den Titel an")){//After this line follows the textbox, works just as the first test.
                line = br.readLine();
                header=line;
            }
        }
    } catch (MalformedURLException mue) {
         mue.printStackTrace();
    } catch (IOException ioe) {
         ioe.printStackTrace();
    } finally {
        try {
            if (is != null) is.close();
        } catch (IOException ioe) {
        }
    }
    System.out.println(filterH(header));
    System.out.println(filterT(textbox));//to be implemented.
}
// Simple Addon still
exports.main = function() {};
require("widget").Widget({
id: "widgetID1",
label: "My Mozilla Widget", //to be changed
contentURL: "http://www.mozilla.org/favicon.ico" // To be changed
 onClick: function(event) {
    var x= document.documentElement.innerHTML;
  //do Filter and Stuff
}
});

尝试在URL中嵌入凭据为:

http://username:password@www.example.com/page.html