问题
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