简体   繁体   English

从Java调用网页上的Javascript

[英]Calling Javascript on web page from Java

My goal is to connect to an OWA page (Microsoft Office Outlook Web Access - basically an email client) and log-in, then read the new page loaded and find the inbox count. 我的目标是连接到OWA页面(Microsoft Office Outlook Web Access - 基本上是电子邮件客户端)并登录,然后读取加载的新页面并查找收件箱计数。

To login, I need to fill the username and the password fields and call a certain javascript function for which I know the name and header. 要登录,我需要填写用户名和密码字段,并调用我知道名称和标题的某个javascript函数。

How do I: 我如何:

  1. Get the DOM for the page? 获取页面的DOM?
  2. Update the DOM to fill out the input text fields? 更新DOM以填写输入文本字段?
  3. Call that Javascript function? 调用Javascript函数?
  4. Get the new URL for the page I am redirected to? 获取我重定向到的页面的新URL?

So far I am able to connect to a webpage and load its page source using the following Java code: 到目前为止,我可以使用以下Java代码连接到网页并加载其页面源:

                // open the connection to the welcome page
                callback.status("Opening connection...");
                URLConnection connection = null;
                try
                {
                    connection = url.openConnection();
                }
                catch(IOException ex)
                {
                    throw new Exception("I/O Problem while attempting URL connection");
                }

                connection.setDoInput(true);

                // open input stream to read website
                callback.status("Opening data stream...");
                InputStream input = null;
                try
                {
                    input = connection.getInputStream();
                }
                catch(IOException ex)
                {
                    throw new Exception("I/O Problem while opening data stream");
                }

                // read website contents
                callback.status("Reading site...");

                String content = "";
                byte[] buffer = new byte[100];
                int totalBytesRead = 0;
                int bytesRead = 0;
                try
                {
                    while((bytesRead = input.read(buffer)) != -1)
                    {
                        String newContent = new String(buffer, 0, bytesRead);
                        content += newContent;
                    }
                }
                catch(IOException ex)
                {
                    throw new Exception("I/O Problem while reading website");
                }

                System.out.println(content);

The result is the entire page source being output to the console - great. 结果是整个页面源输出到控制台 - 很棒。 I also attempted to parse the page to get a DOM object which I can then follow to find my username and password fields: 我还尝试解析页面以获取DOM对象,然后我可以按照该对象查找用户名和密码字段:

                XMLParserConfiguration config = new XML11DTDConfiguration();
                DOMParser parser = new DOMParser(config);
                InputSource inputSource = new InputSource(input);
                inputSource.setByteStream(input);
                try
                {
                    parser.parse(inputSource);
                }
                catch(SAXParseException ex)
                {

                }
                Document document = parser.getDocument();
                visitNode(document, 0);

But I am getting a SAXParseException: :6:62: White spaces are required between publicId and systemId. 但我得到一个SAXParseException :: 6:62:publicId和systemId之间需要空格。

Looks like this line is to blame: 看起来这一行应该归咎于:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

So I may need to change that DOMParser's configuration somehow to be lenient enough and "forgive" the white space requirement. 所以我可能需要以某种方式改变DOMParser的配置以使其足够宽松并“原谅”空白空间要求。

So you want to act like a GUI-less webbrowser programmaticaly? 所以你想要像无GUI的webbrowser programmaticaly一样? Use HtmlUnit , that's exactly what it advertises itself with. 使用HtmlUnit ,这正是它所宣传的内容。

HtmlUnit is a "GUI-Less browser for Java programs". HtmlUnit是一个“用于Java程序的GUI-Less浏览器”。 It models HTML documents and provides an API that allows you to invoke pages, fill out forms, click links, etc... just like you do in your "normal" browser. 它模拟HTML文档,并提供一个API,允许您调用页面,填写表单,单击链接等...就像在“普通”浏览器中一样。

It has fairly good JavaScript support (which is constantly improving) and is able to work even with quite complex AJAX libraries, simulating either Firefox or Internet Explorer depending on the configuration you want to use. 它具有相当好的JavaScript支持(不断改进),即使使用非常复杂的AJAX库也可以工作,根据您要使用的配置模拟Firefox或Internet Explorer。

It is typically used for testing purposes or to retrieve information from web sites. 它通常用于测试目的或从网站检索信息。

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM