How to get HTML from the browser in SWT after setting the URL

若如初见. 提交于 2019-12-22 10:10:10

问题


I have tried using

String html = browser.getText();

But I am getting this error

Exception in thread "main" org.eclipse.swt.SWTException: Failed to change Variant type result = -2147352571 at org.eclipse.swt.ole.win32.OLE.error(Unknown Source) at org.eclipse.swt.ole.win32.Variant.getAutomation(Unknown Source) at org.eclipse.swt.browser.IE.getText(Unknown Source) at org.eclipse.swt.browser.Browser.getText(Unknown Source)

I have read this bug report: https://bugs.eclipse.org/bugs/show_bug.cgi?id=433526

Could anyone help me with other way to get HTML out of the browser?


回答1:


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

For example

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

or

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

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


来源:https://stackoverflow.com/questions/38272812/how-to-get-html-from-the-browser-in-swt-after-setting-the-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!