且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

设置URL后如何从SWT中的浏览器获取HTML

更新时间:2023-01-13 19:50:28

您可以强制SWT使用其他浏览器引擎(如果有)来解决此错误。

You could force SWT to use another browser engine (if available) to work around this bug.

例如

Browser browser = new Browser( parent, SWT.WEBKIT );

Browser browser = new Browser( parent, SWT.MOZILLA );

问题的根源在于,您试图在获取页面源之前完全读取。如果没有您提到的错误,SWT仍将返回一个空字符串。

The source of the problem, however, is that you are trying to obtain the page source before it was fully loaded. If there wasn't the bug that you mentioned, SWT would still return an empty string.

解决方法是监听页面完成加载,然后询问浏览器返回页面源。例如:

The fix is to listen for the page to complete loading and only then ask the browser to return the page source. For example:

Browser browser = new Browser( shell, SWT.NONE );
browser.addProgressListener( new ProgressAdapter() {
  @Override
  public void completed( ProgressEvent event ) {
    String text = browser.getText();
    // here, text will contain the full page source
  }
} );
browser.setUrl( "http://eclipse.org" );