Calling Javascript on a Java Web Page

My goal is to connect to the OWA page (Microsoft Office Outlook Web Access - basically an email client) and log in, then read the new page and find the number of incoming mailboxes.

To enter the system I need to fill in the username and password fields and call a specific javascript function, for which I know the name and title.

Like me:

  • Get DOM for page?
  • Refresh DOM to fill in the entered text fields?
  • Call this Javascript function?
  • Get a new URL for the page I'm redirected to?

So far, I can connect to the web page and load its page source using the following Java code:

                // 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);

- . , 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);

SAXParseException:: 6: 62: publicId systemId .

, :

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

DOMParser -, "" .

+3
1

, - - ? HtmlUnit, .

HtmlUnit - " GUI-Less Java-". HTML- API, , , ..... , "" .

JavaScript ( ) AJAX, Firefox Internet Explorer , .

-.

. :

+11

Source: https://habr.com/ru/post/1755346/


All Articles