0

I want to show a certain webpage in a JavaFx WebView, but it remains blank.

I have add a documentListener to the WebView's WebEngine. This shows me the document is loaded.

I digged in the log at finest level, but I cannot see why the page gets not rendered.

Here is the full code:


import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class FxBrowserOfficeTest extends Application
{
  @Override
  public void start( Stage stage ) throws Exception
  {
    StackPane pane = new StackPane();
    WebView view = new WebView();

    WebEngine engine = view.getEngine();

    engine.load( "https://login.microsoftonline.com/47ef05e2-eef7-412f-b794-3b99f6ab08e7/saml2" );

    engine.documentProperty().addListener( new ChangeListener<Document>()
    {
      @Override
      public void changed( ObservableValue<? extends Document> observable, Document oldDocument, Document newDocument )
      {
        Document doc = engine.getDocument();
        if ( doc != null )
        {
          try
          {
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
            transformer.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount", "4" );
            DOMSource source = new DOMSource( doc );
            ByteArrayOutputStream streamOut = new ByteArrayOutputStream();
            StreamResult result = new StreamResult( streamOut );
            transformer.transform( source, result );

            System.out.println( streamOut.toString().trim() );
          }
          catch ( Exception e )
          {
            e.printStackTrace();
          }
        }
      }
    } );

//ADDED
    System.err.println( "UserAgent: " + engine.getUserAgent() );
    System.err.println( "JavaScriptEnabled: " + engine.isJavaScriptEnabled() );

    engine.getLoadWorker().stateProperty().addListener( new ChangeListener<Worker.State>()
    {
      @Override
      public void changed( ObservableValue ov, Worker.State oldState, Worker.State newState )
      {
        System.err.println( "LoadWorker newState: " + newState );
      }
    } );
// END ADDED


    pane.getChildren().add( view );

    Scene scene = new Scene( pane, 960, 600 );
    stage.setScene( scene );
    stage.show();
  }

  public static void main( String[] args ) throws IOException
  {
    Application.launch( args );
  }
}

I expected the microsoft login page gets rendered, but it remains blank.

Does anybody have a clue why it remains blank?

Olaf
  • 146
  • 8
  • If you observe the load `Worker` do you see it fail? – Slaw Sep 30 '19 at 08:44
  • `engine.setUserAgent` to lie that we are not a robot and (not for this code) `engine.setJavaScriptEnabled` (default true). – Joop Eggen Sep 30 '19 at 08:47
  • 1
    @Slaw I have add a loadworker listener. This printed out: `LoadWorker newState: SUCCEEDED` – Olaf Sep 30 '19 at 09:12
  • @JoopEggen I have edit the code, it prints out: - `UserAgent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/605.1 (KHTML, like Gecko) JavaFX/8.0 Safari/605.1` - `JavaScriptEnabled: true` – Olaf Sep 30 '19 at 09:24
  • So I am wrong. You could save the URL's page via a browser or **curl** tool, and check that (also in the application using a `file:` URL). – Joop Eggen Sep 30 '19 at 09:31
  • @JoopEggen . The html document has got only javascript inside the body. Is there a way to get an error/warning when some javascript code failes. I tried saving the web page in my browser. and show it in the javafx webview using a file urI. mentioned there was some more html code in the body now. The page is not working, but ot shows something. – Olaf Sep 30 '19 at 13:47
  • Possible duplicate: https://stackoverflow.com/questions/52747674/javafx-open-login-microsoftonline-com-page-in-webview-component – SedJ601 Sep 30 '19 at 14:31
  • 1
    https://github.com/mguessan/davmail/issues/12 – SedJ601 Sep 30 '19 at 14:32

0 Answers0