设置URL后如何从SWT中的浏览器获取HTML [英] How to get HTML from the browser in SWT after setting the URL

查看:125
本文介绍了设置URL后如何从SWT中的浏览器获取HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用

String html = browser.getText();

但是我遇到了这个错误


线程 main org.eclipse.swt.SWTException中的异常:无法在org.eclipse.swt.ole.win32.OLE.error上更改变量类型结果= -2147352571
来源)org.eclipse.swt.ole.win32.Variant.getAutomation(未知来源)
(org.eclipse.swt.browser.IE.getText(未知来源)
(org) .eclipse.swt.browser.Browser.getText(未知来源)

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)

我已阅读以下错误报告: https://bugs.eclipse.org/bugs/show_bug.cgi?id=433526

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

有人可以用其他方法帮助我从浏览器中获取HTML吗?

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

推荐答案

您可以强制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" );         

这篇关于设置URL后如何从SWT中的浏览器获取HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆